Python Pandas: How to Remove Time from DateTime (Extract Date Only)
When working with date and time data in Pandas, you often encounter datetime
objects that include both date and time components (e.g., '2025-10-27 15:30:45'). For certain analyses or presentations, you might only need the date part, effectively removing or ignoring the time information.
This guide explains several common methods to extract just the date component from a Pandas Series or DataFrame column containing datetime objects, or from single Timestamp
objects.
The Goal: Isolating the Date Component
Given a Pandas Series or DataFrame column containing full datetime
objects (which include year, month, day, hour, minute, second, etc.), our objective is to obtain a representation that only includes the date part (year, month, day), effectively "removing" the time information.
Example DataFrame:
import pandas as pd
data = {
'EventID': [1, 2, 3, 4],
'Timestamp': [
'2025-01-15 10:30:00',
'2025-05-20 23:59:59',
'2025-08-10 00:00:00',
'2026-02-20 12:00:15.500' # Includes milliseconds
],
'Value': [100, 150, 200, 50]
}
df_original = pd.DataFrame(data)
print("Original DataFrame:")
print(df_original)
print()
print("Original dtypes:")
print(df_original.dtypes)
Output:
Original DataFrame:
EventID Timestamp Value
0 1 2025-01-15 10:30:00 100
1 2 2025-05-20 23:59:59 150
2 3 2025-08-10 00:00:00 200
3 4 2026-02-20 12:00:15.500 50
Original dtypes:
EventID int64
Timestamp object
Value int64
dtype: object
Prerequisite: Ensuring Datetime Type
Most Pandas date/time operations require the column to be of datetime64[ns]
(datetime) type. If your 'Timestamp' column is stored as strings (object dtype), convert it first.
import pandas as pd
df = pd.DataFrame({
'EventID': [1, 2, 3, 4],
'Timestamp': [
'2025-01-15 10:30:00', '2025-05-20 23:59:59',
'2025-08-10 00:00:00', '2026-02-20 12:00:15.500'
],
'Value': [100, 150, 200, 50]
})
# ✅ Convert 'Timestamp' column to datetime objects
df['Timestamp'] = pd.to_datetime(df['Timestamp'], format='mixed')
print("DataFrame after converting 'Timestamp' to datetime:")
print(df)
print()
print("Dtypes after conversion:")
print(df.dtypes)
Output:
DataFrame after converting 'Timestamp' to datetime:
EventID Timestamp Value
0 1 2025-01-15 10:30:00.000 100
1 2 2025-05-20 23:59:59.000 150
2 3 2025-08-10 00:00:00.000 200
3 4 2026-02-20 12:00:15.500 50
Dtypes after conversion:
EventID int64
Timestamp datetime64[ns]
Value int64
dtype: object
Method 1: Using Series.dt.date
(Returns Python datetime.date
objects)
Once your column is of datetime64
type, the .dt
accessor provides access to datetime-like properties. Series.dt.date
extracts the date part as Python datetime.date
objects. The resulting column will have object
dtype because it now holds these Python objects.
import pandas as pd
# ✅ Use ISO8601 format parsing which handles milliseconds automatically
df = pd.DataFrame({
'EventID': [1, 2, 3, 4],
'Timestamp': pd.to_datetime([
'2025-01-15 10:30:00',
'2025-05-20 23:59:59',
'2025-08-10 00:00:00',
'2026-02-20 12:00:15.500'
], format='ISO8601'), # <-- this is key
'Value': [100, 150, 200, 50]
})
# ✅ Extract the date part using .dt.date
df['Date_Only_dt_date'] = df['Timestamp'].dt.date
print("DataFrame with date extracted using .dt.date:")
print(df)
print()
print("Dtypes including new column:")
print(df.dtypes)
Output:
DataFrame with date extracted using .dt.date:
EventID Timestamp Value Date_Only_dt_date
0 1 2025-01-15 10:30:00.000 100 2025-01-15
1 2 2025-05-20 23:59:59.000 150 2025-05-20
2 3 2025-08-10 00:00:00.000 200 2025-08-10
3 4 2026-02-20 12:00:15.500 50 2026-02-20
Dtypes including new column:
EventID int64
Timestamp datetime64[ns]
Value int64
Date_Only_dt_date object
dtype: object
This method effectively "removes" the time by converting to a datetime.date
object, which only stores year, month, and day.
Method 2: Using Series.dt.normalize()
(Sets Time to Midnight, Keeps datetime64
type)
If you want to keep the column as datetime64[ns]
type but set the time component to midnight (00:00:00) for all dates, use Series.dt.normalize()
. This is useful if you still need datetime functionality but want to ignore the time part for comparisons or grouping.
import pandas as pd
df = pd.DataFrame({
'EventID': [1, 2, 3, 4],
'Timestamp': pd.to_datetime([
'2025-01-15 10:30:00', '2025-05-20 23:59:59',
'2025-08-10 00:00:00', '2026-02-20 12:00:15.500'
], format='ISO8601'),
'Value': [100, 150, 200, 50]
})
# ✅ Normalize to midnight, keeping datetime64 type
df['Date_Normalized'] = df['Timestamp'].dt.normalize()
print("DataFrame with date normalized to midnight:")
print(df[['Timestamp', 'Date_Normalized']]) # Show original and normalized
print()
print("Dtypes including new column:")
print(df.dtypes)
Output:
DataFrame with date normalized to midnight:
Timestamp Date_Normalized
0 2025-01-15 10:30:00.000 2025-01-15
1 2025-05-20 23:59:59.000 2025-05-20
2 2025-08-10 00:00:00.000 2025-08-10
3 2026-02-20 12:00:15.500 2026-02-20
Dtypes including new column:
EventID int64
Timestamp datetime64[ns]
Value int64
Date_Normalized datetime64[ns]
dtype: object
When displayed, Pandas might omit the 00:00:00
part if all times in the column are midnight, but the underlying data is still datetime64[ns]
.
Method 3: Using Series.dt.strftime('%Y-%m-%d')
(Returns Formatted Strings)
If you want the date part as a string in a specific format (e.g., "YYYY-MM-DD"), use Series.dt.strftime(date_format)
. The resulting column will be of object
(string) dtype.
import pandas as pd
df = pd.DataFrame({
'EventID': [1, 2, 3, 4],
# ✅ Use ISO8601 parsing to handle the millisecond format safely
'Timestamp': pd.to_datetime([
'2025-01-15 10:30:00',
'2025-05-20 23:59:59',
'2025-08-10 00:00:00',
'2026-02-20 12:00:15.500'
], format='ISO8601'),
'Value': [100, 150, 200, 50]
})
# ✅ Format as "YYYY-MM-DD" string
date_format_str = '%Y-%m-%d'
df['Date_String_Formatted'] = df['Timestamp'].dt.strftime(date_format_str)
print("DataFrame with date formatted as string:")
print(df[['Timestamp', 'Date_String_Formatted']])
print()
print("Dtypes including new column:")
print(df.dtypes)
Output:
DataFrame with date formatted as string:
Timestamp Date_String_Formatted
0 2025-01-15 10:30:00.000 2025-01-15
1 2025-05-20 23:59:59.000 2025-05-20
2 2025-08-10 00:00:00.000 2025-08-10
3 2026-02-20 12:00:15.500 2026-02-20
Dtypes including new column:
EventID int64
Timestamp datetime64[ns]
Value int64
Date_String_Formatted object
dtype: object
This method gives you full control over the output string format of the date.
Method 4: String Manipulation (For String Columns - Use with Caution)
If your date column is already strings and you don't want to convert it to datetime
objects first (perhaps due to performance concerns on massive datasets or known consistent string format), you can use string methods. This is generally less robust than datetime conversion.
import pandas as pd
data_str_dates = {
'Timestamp_Str': [
'2025-01-15 10:30:00',
'2025-05-20 08:25:44',
'2025-09-21 07:15:33'
]
}
df_str = pd.DataFrame(data_str_dates)
print("Original DataFrame with string dates:")
print(df_str)
print()
# ✅ Split string by space and take the first part (the date)
# This assumes a consistent format "YYYY-MM-DD HH:MM:SS"
df_str['Date_Part_Str'] = df_str['Timestamp_Str'].str.split(' ').str[0]
# Alternatively, fixed-width slice if format is very rigid:
# df_str['Date_Part_Str'] = df_str['Timestamp_Str'].str[:10]
print("DataFrame with date part extracted using string split:")
print(df_str)
Output:
Original DataFrame with string dates:
Timestamp_Str
0 2025-01-15 10:30:00
1 2025-05-20 08:25:44
2 2025-09-21 07:15:33
DataFrame with date part extracted using string split:
Timestamp_Str Date_Part_Str
0 2025-01-15 10:30:00 2025-01-15
1 2025-05-20 08:25:44 2025-05-20
2 2025-09-21 07:15:33 2025-09-21
Caution: This method is fragile. It breaks if date formats vary, if there are extra spaces, or if some strings don't contain a time part. Converting to datetime
(Method 1, 2, or 3) is usually safer.
Removing Time from a Single Pandas Timestamp
Object
If you have an individual Pandas Timestamp
object:
import pandas as pd
single_timestamp = pd.Timestamp('2026-03-15 14:45:30')
print(f"Original single Timestamp: {single_timestamp}")
# ✅ Get the date part as a Python datetime.date object
date_part_only = single_timestamp.date()
print(f"Date part only (.date()): {date_part_only}")
print(f"Type of date_part_only: {type(date_part_only)}")
# ✅ Normalize to midnight (returns another Timestamp object)
normalized_timestamp = single_timestamp.normalize()
print(f"Normalized to midnight (.normalize()): {normalized_timestamp}")
# ✅ Format as string
formatted_date_str = single_timestamp.strftime('%Y-%m-%d')
print(f"Formatted as string (.strftime()): {formatted_date_str}")
Output:
Original single Timestamp: 2026-03-15 14:45:30
Date part only (.date()): 2026-03-15
Type of date_part_only: <class 'datetime.date'>
Normalized to midnight (.normalize()): 2026-03-15 00:00:00
Formatted as string (.strftime()): 2026-03-15
Conclusion
To effectively "remove" the time component from datetime data in a Pandas Series:
- Ensure the Series is of
datetime64[ns]
type usingpd.to_datetime()
. - Choose your method based on the desired output type:
Series.dt.date
: Returns Pythondatetime.date
objects (column becomesobject
dtype). This truly removes the time component.Series.dt.normalize()
: Sets the time to midnight (00:00:00) but keeps thedatetime64[ns]
dtype. Useful for date-level comparisons while retaining datetime capabilities.Series.dt.strftime('%Y-%m-%d')
: Converts to a string representation of the date in your chosen format (column becomesobject
dtype).
- For columns already stored as strings (and if
pd.to_datetime()
is to be avoided), basic string manipulation like.str.split()
can be used, but with caution due to format dependency.
Select the method that best suits your analytical needs, considering the desired data type of the resulting column and how you intend to use the date-only information.