Course Content
Dealing with Dates and Times in Python
Dealing with Dates and Times in Python
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:
pd.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).
Task
- Get the first
pickup_datetime
object and save it in 'dt_before' variable. - Convert columns
pickup_datetime
anddropoff_datetime
into datetime type. To do it, reassign to respective column result of applying.to_datetime
function to the same column. - Extract the first
pickup_datetime
after conversion and save it indt_after
variable.
Thanks for your feedback!
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:
pd.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).
Task
- Get the first
pickup_datetime
object and save it in 'dt_before' variable. - Convert columns
pickup_datetime
anddropoff_datetime
into datetime type. To do it, reassign to respective column result of applying.to_datetime
function to the same column. - Extract the first
pickup_datetime
after conversion and save it indt_after
variable.
Thanks for your feedback!
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:
pd.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).
Task
- Get the first
pickup_datetime
object and save it in 'dt_before' variable. - Convert columns
pickup_datetime
anddropoff_datetime
into datetime type. To do it, reassign to respective column result of applying.to_datetime
function to the same column. - Extract the first
pickup_datetime
after conversion and save it indt_after
variable.
Thanks for your feedback!
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:
pd.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).
Task
- Get the first
pickup_datetime
object and save it in 'dt_before' variable. - Convert columns
pickup_datetime
anddropoff_datetime
into datetime type. To do it, reassign to respective column result of applying.to_datetime
function to the same column. - Extract the first
pickup_datetime
after conversion and save it indt_after
variable.