In this tutorial, you’ll learn how to use Pandas to extract date parts from a datetime column, such as to date, year, and month. Pandas provides a number of easy ways to extract parts from a datetime object, including using the .dt
accessor.
By the end of this tutorial, you’ll have learned how the dt
accessor works and how to use the normalize
function to convert a column to a date while maintaining the datetime data type. You’ll also learn how to check the data types of other columns and a general overview of common date parts you may want to convert to. You could also simply use the Python .strftime()
function, but it’s always good to have multiple ways to handle a problem.
The Quick Answer: Use df[‘date_column’].dt.date To Extract Date from Pandas Datetime
# Extract date from datetime column in Pandas
df['Date'] = df['DateTime'].dt.date
Table of Contents
What is the Pandas Datetime dt Accessor
When working with Pandas datetime values, we can use the .dt
accessor to access different attributes from a Pandas series. This means that we can extract different parts from a datetime object, such as months, date, and more.
The accessor works on columns of type datetime64[ns]
and allows us to access the vast amounts of data. When we apply the accessor on a series, the values returned are a series with the same indices as the one applied to it. This allows us to easily extract datetime like values for an entire column. What’s more, is that we can then easily filter our dataframe based on these values.
In the next section, you’ll see a number of common datetime accessors you can use in Pandas.
Common Datetime Accessors to Extract in Pandas
The following tables provides an overview of some of the most common dt accessors you can use in Pandas:
Pandas dt accessor | Used to access |
---|---|
.week | The week number |
.year | The year value, returned as an integer |
.date | The date without time values |
.day | The day of the month, returned as a value from 1 through 31 |
.month | The month of the year, returned as a value from 1 through 12 |
.weekday | The day of the week returned as a value where Monday=0 and Sunday=6 |
.day_name() | The name of the weekday returned as a string |
dt
accessors in PandasIn the next section, you’ll learn how to use the Pandas .dt
accessor to convert Pandas columns to datetime like values.
Loading a Sample Pandas Dataframe
In order to follow along with this tutorial, I have provided a sample Pandas Dataframe. Feel free to copy the code below into your favourite code editor. If you want to follow along with your own dataset, your results will of course vary.
# Loading a Sample Pandas Dataframe
import pandas as pd
df = pd.DataFrame.from_dict({
'DateTime': ['2022-01-01 15:34:21', '2022-02-03 10:13:45', '2022-03-04 12:12:45', '2022-04-03 14:45:23', '2022-05-27 18:23:45'],
'Name': ['Nik', 'Kate', 'Lou', 'Samrat', 'Jim'],
'Age': [33, 32, 45, 37, 23]
})
df['DateTime'] = pd.to_datetime(df['DateTime'])
print(df)
# Returns:
# DateTime Name Age
# 0 2022-01-01 15:34:21 Nik 33
# 1 2022-02-03 10:13:45 Kate 32
# 2 2022-03-04 12:12:45 Lou 45
# 3 2022-04-03 14:45:23 Samrat 37
# 4 2022-05-27 18:23:45 Jim 23
We can see that we have three columns, one of which contains datetime values. We can check the type of this column by using the .dtype
property:
# Checking the data type of our DateTime column
print(df['DateTime'].dtype)
# Returns: datetime64[ns]
Now that we have our dataframe loaded, let’s begin by learning how to extract a date from a datetime column.
Extract a Date from a Pandas Datetime Column
In many cases, you’ll want to extract just a date from a datetime column. The .dt.date
function makes this very easy and allows us to extract just the date from a column.
Let’s see what this looks like:
# Extract date from datetime column in Pandas
df['Date'] = df['DateTime'].dt.date
print(df)
# Returns:
# DateTime Name Age Date
# 0 2022-01-01 15:34:21 Nik 33 2022-01-01
# 1 2022-02-03 10:13:45 Kate 32 2022-02-03
# 2 2022-03-04 12:12:45 Lou 45 2022-03-04
# 3 2022-04-03 14:45:23 Samrat 37 2022-04-03
# 4 2022-05-27 18:23:45 Jim 23 2022-05-27
We can see how easy it was to extract just the date portion from a datetime column.
Something important to note is that the date that’s returned is actually an object
datatype. We can confirm this by checking the data type of the column:
# Checking the data type of the returned column
df['Date'] = df['DateTime'].dt.date
print(df['Date'].dtype)
# Returns: object
This may or not work for your use cases. While you could convert the date object back to a date, we can actually retain the data type directly by using the dt.normalize()
function.
Use Normalize to a Extract a Date from a Pandas Datetime Column
In order to maintain the data type of our column when extracting a date from a Pandas column, we can use the .dt.normalize()
function. The function takes a date time object and normalizes the time to midnight. This is a useful method that allows us to keep the data type when time information is not important.
Let’s see how we can use this method to extract a date from a datetime column:
# Extract date from datetime column in Pandas
df['Date'] = df['DateTime'].dt.normalize()
print(df)
# Returns:
# DateTime Name Age Date
# 0 2022-01-01 15:34:21 Nik 33 2022-01-01
# 1 2022-02-03 10:13:45 Kate 32 2022-02-03
# 2 2022-03-04 12:12:45 Lou 45 2022-03-04
# 3 2022-04-03 14:45:23 Samrat 37 2022-04-03
# 4 2022-05-27 18:23:45 Jim 23 2022-05-27
We can see that by applying the normalize function that the date was extracted. Let’s confirm that the data type still remained the same:
df['Date'] = df['DateTime'].dt.normalize()
print(df['Date'].dtype)
# Returns: datetime64[ns]
We can see that when using the .dt.normalize()
function that the resultant data type is not an object
, but remained as a datetime64[ns]
data type.
In the next section, you’ll learn how to use the .dt
accessor to access different parts from a datetime object.
Extract Date Parts from a Pandas Datetime Column
In this section, you’ll see how you can use Pandas to extract different date parts from a datetime column. There are a number of different options that you can pull from a datetime column.
Extract a Month from a Pandas Datetime Column
Because month’s can be presented in a number of different ways, we should learn how they can best be extracted.
We can use the following accessors:
.month
will return the month as a number from 1 through 12.month_name()
will return the locale’s named month, allowing you to pass in a different locale.month_name().str[:3]
will return the abbreviated version of the month’s name
Let’s see what this looks like:
# Extract month from datetime column in Pandas
df['MonthNum'] = df['DateTime'].dt.month
df['MonthName'] = df['DateTime'].dt.month_name()
df['MonthNameShort'] = df['DateTime'].dt.month_name().str[:3]
print(df)
# Returns:
# DateTime Name Age MonthNum MonthName MonthNameShort
# 0 2022-01-01 15:34:21 Nik 33 1 January Jan
# 1 2022-02-03 10:13:45 Kate 32 2 February Feb
# 2 2022-03-04 12:12:45 Lou 45 3 March Mar
# 3 2022-04-03 14:45:23 Samrat 37 4 April Apr
# 4 2022-05-27 18:23:45 Jim 23 5 May May
In the next section you’ll learn how to extract a weekday from a Pandas datetime column.
Extract a Weekday from a Pandas Datetime Column
There are also many different ways to represent weekdays. For example, we can represent them as a number – but even this can be done in different ways. Otherwise, they can be represented as a string of the weekday’s name. Finally, they can be represented as the day’s number in the month or in the year.
We can use the following attributes to access weekday information:
.weekday
returns the weekday number starting at 0 for Mondaysday_name()
returns the name of the day based a given locale.day
returns the number of days into the month the day is.dayofyear
returns the number of days into the year the day is
Let’s see how we can use these to extract information:
# Extract weekday from datetime column in Pandas
df['Weekday'] = df['DateTime'].dt.weekday
df['Name'] = df['DateTime'].dt.day_name()
df['NameShort'] = df['DateTime'].dt.day_name().str[:3]
df['DayOfMonth'] = df['DateTime'].dt.day
df['DayOfYear'] = df['DateTime'].dt.dayofyear
print(df)
# Returns:
# DateTime Name Age Weekday NameShort DayOfMonth DayOfYear
# 0 2022-01-01 15:34:21 Saturday 33 5 Sat 1 1
# 1 2022-02-03 10:13:45 Thursday 32 3 Thu 3 34
# 2 2022-03-04 12:12:45 Friday 45 4 Fri 4 63
# 3 2022-04-03 14:45:23 Sunday 37 6 Sun 3 93
# 4 2022-05-27 18:23:45 Friday 23 4 Fri 27 147
Conclusion
In this tutorial, you learned how to use Pandas to extract a date from a datetime column using the dt
accessor. You learned how these accessors work and the types of data that they return. You also learned how to extract month information from a datetime column as well as weekday information.
To learn more about the Pandas dt
accessor, check out the official documentation here.
Additional Resources
To learn about related topics, check out these tutorials:
Pingback: DateTime in Pandas and Python • datagy