Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Converting Columns into datetime Type | Working with Dates and Times in pandas
Dealing with Dates and Times in Python
course content

Course Content

Dealing with Dates and Times in Python

Dealing with Dates and Times in Python

1. Working with Dates
2. Working with Times
3. Timezones and Daylight Savings Time (DST)
4. Working with Dates and Times in pandas

bookChallenge: 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:

1
pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
copy

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

  1. Get the first pickup_datetime object and save it in 'dt_before' variable.
  2. Convert columns pickup_datetime and dropoff_datetime into datetime type. To do it, reassign to respective column result of applying .to_datetime function to the same column.
  3. Extract the first pickup_datetime after conversion and save it in dt_after variable.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookChallenge: 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:

1
pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
copy

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

  1. Get the first pickup_datetime object and save it in 'dt_before' variable.
  2. Convert columns pickup_datetime and dropoff_datetime into datetime type. To do it, reassign to respective column result of applying .to_datetime function to the same column.
  3. Extract the first pickup_datetime after conversion and save it in dt_after variable.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookChallenge: 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:

1
pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
copy

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

  1. Get the first pickup_datetime object and save it in 'dt_before' variable.
  2. Convert columns pickup_datetime and dropoff_datetime into datetime type. To do it, reassign to respective column result of applying .to_datetime function to the same column.
  3. Extract the first pickup_datetime after conversion and save it in dt_after variable.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

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:

1
pd.to_datetime(arg, dayfirst = False, yearfirst = False, format = None, exact = True, ...)
copy

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

  1. Get the first pickup_datetime object and save it in 'dt_before' variable.
  2. Convert columns pickup_datetime and dropoff_datetime into datetime type. To do it, reassign to respective column result of applying .to_datetime function to the same column.
  3. Extract the first pickup_datetime after conversion and save it in dt_after variable.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt