Python Pandas: How to Convert Datetime to Date
When working with datetime data in pandas, you'll often need to extract just the date portion and discard the time component. This is common when aggregating data by day, joining datasets on dates, creating reports, or simplifying timestamps for display purposes.
In this guide, you'll learn multiple methods to convert datetime values to dates in pandas, understand the differences between them, and choose the right approach for your use case.
Setting Up the Example
Let's create a sample DataFrame with a column containing datetime strings:
import pandas as pd
df = pd.DataFrame({
'DateTime': [
'2021-01-15 20:02:11',
'1989-05-24 20:34:11',
'2020-01-18 14:43:24',
'2021-01-15 08:15:30',
'1999-04-04 20:34:11'
]
})
# Convert string column to datetime type
df['DateTime'] = pd.to_datetime(df['DateTime'])
print(df)
print(f"\nDtype: {df['DateTime'].dtype}")
Output:
DateTime
0 2021-01-15 20:02:11
1 1989-05-24 20:34:11
2 2020-01-18 14:43:24
3 2021-01-15 08:15:30
4 1999-04-04 20:34:11
Dtype: datetime64[ns]
Using .dt.date to Extract the Date
The .dt.date accessor extracts the date component from each datetime value, returning Python datetime.date objects:
import pandas as pd
df = pd.DataFrame({
'DateTime': pd.to_datetime([
'2021-01-15 20:02:11',
'1989-05-24 20:34:11',
'2020-01-18 14:43:24',
'2021-01-15 08:15:30',
'1999-04-04 20:34:11'
])
})
# Extract date only
df['Date'] = df['DateTime'].dt.date
print(df)
print(f"\nDateTime dtype: {df['DateTime'].dtype}")
print(f"Date dtype: {df['Date'].dtype}")
Output:
DateTime Date
0 2021-01-15 20:02:11 2021-01-15
1 1989-05-24 20:34:11 1989-05-24
2 2020-01-18 14:43:24 2020-01-18
3 2021-01-15 08:15:30 2021-01-15
4 1999-04-04 20:34:11 1999-04-04
DateTime dtype: datetime64[ns]
Date dtype: object
dt.date Returns Python Objects, Not datetime64The resulting Date column has dtype object because it contains Python datetime.date objects, not pandas datetime64 values. This means you lose some pandas datetime functionality like .dt accessor methods on the new column.
For most display and comparison purposes, this works perfectly. If you need to maintain pandas datetime functionality, use dt.normalize() instead (shown below).
Using .dt.normalize() to Remove the Time Component
The normalize() method sets the time component to midnight (00:00:00) while keeping the column as datetime64 dtype. This preserves full pandas datetime functionality:
import pandas as pd
df = pd.DataFrame({
'DateTime': pd.to_datetime([
'2021-01-15 20:02:11',
'1989-05-24 20:34:11',
'2020-01-18 14:43:24'
])
})
df['Date'] = df['DateTime'].dt.normalize()
print(df)
print(f"\nDate dtype: {df['Date'].dtype}")
Output:
DateTime Date
0 2021-01-15 20:02:11 2021-01-15
1 1989-05-24 20:34:11 1989-05-24
2 2020-01-18 14:43:24 2020-01-18
Date dtype: datetime64[ns]
The Date column retains datetime64[ns] dtype, meaning you can still use all pandas datetime operations on it:
import pandas as pd
df = pd.DataFrame({
'DateTime': pd.to_datetime([
'2021-01-15 20:02:11',
'1989-05-24 20:34:11',
'2020-01-18 14:43:24'
])
})
df['Date'] = df['DateTime'].dt.normalize()
# These work because dtype is still datetime64
print(df['Date'].dt.year)
print(df['Date'].dt.day_name())
Output:
0 2021
1 1989
2 2020
Name: Date, dtype: int32
0 Friday
1 Wednesday
2 Saturday
Name: Date, dtype: object
Using .dt.strftime() for String Formatting
If you need the date as a formatted string (not a date object), use strftime():
import pandas as pd
df = pd.DataFrame({
'DateTime': pd.to_datetime([
'2021-01-15 20:02:11',
'1989-05-24 20:34:11',
'2020-01-18 14:43:24'
])
})
# Various date string formats
df['Date_ISO'] = df['DateTime'].dt.strftime('%Y-%m-%d')
df['Date_US'] = df['DateTime'].dt.strftime('%m/%d/%Y')
df['Date_Long'] = df['DateTime'].dt.strftime('%B %d, %Y')
print(df[['DateTime', 'Date_ISO', 'Date_US', 'Date_Long']])
Output:
DateTime Date_ISO Date_US Date_Long
0 2021-01-15 20:02:11 2021-01-15 01/15/2021 January 15, 2021
1 1989-05-24 20:34:11 1989-05-24 05/24/1989 May 24, 1989
2 2020-01-18 14:43:24 2020-01-18 01/18/2020 January 18, 2020
strftime() returns strings, not date objects. You cannot perform date arithmetic or comparisons on the resulting column without converting back to datetime first.
Using .dt.floor() for Precision Control
The floor() method rounds datetime values down to a specified frequency. Using 'D' (day) effectively removes the time component:
import pandas as pd
df = pd.DataFrame({
'DateTime': pd.to_datetime([
'2021-01-15 20:02:11',
'2021-01-15 08:30:00',
'2021-01-16 23:59:59'
])
})
df['Date'] = df['DateTime'].dt.floor('D')
print(df)
print(f"\nDtype: {df['Date'].dtype}")
Output:
DateTime Date
0 2021-01-15 20:02:11 2021-01-15
1 2021-01-15 08:30:00 2021-01-15
2 2021-01-16 23:59:59 2021-01-16
Dtype: datetime64[ns]
floor('D') produces the same result as normalize() and also preserves the datetime64 dtype.
Comparing the Methods
import pandas as pd
dt = pd.to_datetime(pd.Series(['2021-06-15 14:30:00']))
print(f"dt.date: {dt.dt.date.values[0]} type: {type(dt.dt.date.values[0])}")
print(f"normalize: {dt.dt.normalize().values[0]} type: {dt.dt.normalize().dtype}")
print(f"strftime: {dt.dt.strftime('%Y-%m-%d').values[0]} type: {type(dt.dt.strftime('%Y-%m-%d').values[0])}")
print(f"floor('D'): {dt.dt.floor('D').values[0]} type: {dt.dt.floor('D').dtype}")
Output:
dt.date: 2021-06-15 type: <class 'datetime.date'>
normalize: 2021-06-15T00:00:00.000000000 type: datetime64[ns]
strftime: 2021-06-15 type: <class 'str'>
floor('D'): 2021-06-15T00:00:00.000000000 type: datetime64[ns]
Practical Example: Grouping by Date
A common reason to extract dates from datetimes is to aggregate data by day:
import pandas as pd
import numpy as np
# Sample data with timestamps
np.random.seed(42)
df = pd.DataFrame({
'Timestamp': pd.date_range('2024-01-01', periods=10, freq='6H'),
'Sales': np.random.randint(50, 200, 10)
})
# Extract date for grouping
df['Date'] = df['Timestamp'].dt.normalize()
# Group by date and sum sales
daily_sales = df.groupby('Date')['Sales'].sum().reset_index()
print("Raw data (first 5 rows):")
print(df.head())
print(f"\nDaily totals:")
print(daily_sales)
Output:
Raw data (first 5 rows):
Timestamp Sales Date
0 2024-01-01 00:00:00 152 2024-01-01
1 2024-01-01 06:00:00 142 2024-01-01
2 2024-01-01 12:00:00 64 2024-01-01
3 2024-01-01 18:00:00 156 2024-01-01
4 2024-01-02 00:00:00 121 2024-01-02
Daily totals:
Date Sales
0 2024-01-01 514
1 2024-01-02 514
2 2024-01-03 261
Quick Comparison
| Method | Output Type | Keeps datetime64 | Date Arithmetic | Best For |
|---|---|---|---|---|
.dt.date | datetime.date | ❌ | ❌ | Display, simple extraction |
.dt.normalize() | datetime64[ns] | ✅ | ✅ | Grouping, filtering, joins |
.dt.strftime() | str | ❌ | ❌ | Custom formatted output |
.dt.floor('D') | datetime64[ns] | ✅ | ✅ | Same as normalize, more flexible |
Conclusion
Pandas provides several ways to convert datetime values to dates, each suited to different scenarios:
- Use
.dt.datefor a simple, clean date extraction when you just need the date for display or basic comparisons. - Use
.dt.normalize()or.dt.floor('D')when you need to maintaindatetime64dtype for continued pandas operations like grouping, filtering, or merging. - Use
.dt.strftime()when you need a specific string format for reports, exports, or display.
For most data analysis workflows, .dt.normalize() is the best choice because it strips the time component while preserving the powerful datetime64 dtype that pandas works with most efficiently.