Challenge: Converting Columns into datetime Type
In the previous chapter, you found types of all columns in the dataframe. You might notice, that pickup_datetime and dropoff_datetime columns are recognized as the object type. It means we can not perform the actions we did in the previous sections there.
To fix this problem, we need to convert this column into datetime type. Fortunately, pandas can handle it. There is .to_datetime function available in pandas to do it. This function has the following syntax:
1pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
The arguments above are not exhaustive, but we want to focus on only the most important ones. arg - is the value(s)/column you want to convert, dayfirst and yearfirst - specify if a parse date with day/year first, format - format of datetime object to parse (like in strptime() - you need to define the used format), exact - if True, require an exact match. All arguments but not arg are optional. If you will not specify a format, it will try to guess.
If the dataset is quite large, guessing is not a bad approach. But if you have limited observations, it can become a problem (for example, you may have a date in format 05/07/2019, and there is no exact answer is it 5 July or 7 May).
Swipe to start coding
- Get the first
pickup_datetimeobject and save it in 'dt_before' variable. - Convert columns
pickup_datetimeanddropoff_datetimeinto datetime type. To do it, reassign to respective column result of applying.to_datetimefunction to the same column. - Extract the first
pickup_datetimeafter conversion and save it indt_aftervariable.
Ratkaisu
Kiitos palautteestasi!
single
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Tiivistä tämä luku
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 3.23
Challenge: Converting Columns into datetime Type
Pyyhkäise näyttääksesi valikon
In the previous chapter, you found types of all columns in the dataframe. You might notice, that pickup_datetime and dropoff_datetime columns are recognized as the object type. It means we can not perform the actions we did in the previous sections there.
To fix this problem, we need to convert this column into datetime type. Fortunately, pandas can handle it. There is .to_datetime function available in pandas to do it. This function has the following syntax:
1pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
The arguments above are not exhaustive, but we want to focus on only the most important ones. arg - is the value(s)/column you want to convert, dayfirst and yearfirst - specify if a parse date with day/year first, format - format of datetime object to parse (like in strptime() - you need to define the used format), exact - if True, require an exact match. All arguments but not arg are optional. If you will not specify a format, it will try to guess.
If the dataset is quite large, guessing is not a bad approach. But if you have limited observations, it can become a problem (for example, you may have a date in format 05/07/2019, and there is no exact answer is it 5 July or 7 May).
Swipe to start coding
- Get the first
pickup_datetimeobject and save it in 'dt_before' variable. - Convert columns
pickup_datetimeanddropoff_datetimeinto datetime type. To do it, reassign to respective column result of applying.to_datetimefunction to the same column. - Extract the first
pickup_datetimeafter conversion and save it indt_aftervariable.
Ratkaisu
Kiitos palautteestasi!
single