Python Pandas: How to Convert Pandas Timestamp to Datetime
When working with time-series data in Pandas, you frequently encounter Timestamp objects - Pandas' native representation of a single point in time. However, many Python libraries and APIs expect standard Python datetime objects instead. Converting between these two types is a common task in data processing, logging, and interoperability with non-Pandas code. This guide explains what Pandas Timestamps are, how they differ from Python datetime objects, and covers multiple methods to convert between them.
Understanding Pandas Timestamp vs. Python datetime
A Pandas Timestamp (pd.Timestamp) is Pandas' equivalent of Python's datetime.datetime, but with additional functionality for handling time zones, nanosecond precision, and seamless integration with Pandas DataFrames and Series.
import pandas as pd
from datetime import datetime
ts = pd.Timestamp("2024-06-15 10:30:00")
dt = datetime(2024, 6, 15, 10, 30, 0)
print(f"Pandas Timestamp: {type(ts)}")
print(f"Python datetime: {type(dt)}")
print(f"Are they equal? {ts == dt}")
Output:
Pandas Timestamp: <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Python datetime: <class 'datetime.datetime'>
Are they equal? True
While they compare as equal, they are different types. Some functions or libraries specifically require one or the other.
Method 1: Using to_pydatetime() - Full Datetime Conversion
The to_pydatetime() method converts a Pandas Timestamp into a standard Python datetime.datetime object, preserving all date and time components including timezone information:
import pandas as pd
# Create a Timestamp with specific components
ts = pd.Timestamp(year=2024, month=1, day=28, hour=15, minute=30, second=45, microsecond=123456)
# Convert to Python datetime
datetime_obj = ts.to_pydatetime()
print("Original Timestamp:", ts)
print("Python datetime: ", datetime_obj)
print("Type: ", type(datetime_obj))
Output:
Original Timestamp: 2024-01-28 15:30:45.123456
Python datetime: 2024-01-28 15:30:45.123456
Type: <class 'datetime.datetime'>
All components - year, month, day, hour, minute, second, and microsecond - are preserved in the conversion.
Converting with Timezone Information
to_pydatetime() also preserves timezone data:
import pandas as pd
ts = pd.Timestamp("2024-06-15 10:30:00", tz="US/Eastern")
datetime_obj = ts.to_pydatetime()
print("Timestamp:", ts)
print("Datetime: ", datetime_obj)
print("Timezone: ", datetime_obj.tzinfo)
Output:
Timestamp: 2024-06-15 10:30:00-04:00
Datetime: 2024-06-15 10:30:00-04:00
Timezone: US/Eastern
to_pydatetime() is the recommended method for converting a single Timestamp to a Python datetime. It preserves all information including timezone and microsecond precision.
Method 2: Using date() - Extract Date Only
If you only need the date portion and can discard the time, use the date() method. This returns a datetime.date object:
import pandas as pd
ts = pd.Timestamp("2024-02-05 10:30:45")
date_obj = ts.date()
print("Original Timestamp:", ts)
print("Date only: ", date_obj)
print("Type: ", type(date_obj))
Output:
Original Timestamp: 2024-02-05 10:30:45
Date only: 2024-02-05
Type: <class 'datetime.date'>
Note that datetime.date is not the same as datetime.datetime - it has no time component:
import pandas as pd
ts = pd.Timestamp("2024-02-05 10:30:45")
date_obj = ts.date()
# datetime.date has no hour, minute, or second attributes
print(date_obj.year, date_obj.month, date_obj.day)
Output:
2024 2 5
Method 3: Converting an Entire Column of Timestamps
In practice, you often need to convert a whole column of Timestamps rather than individual values.
Converting a Column to Python datetime Objects
Use the .dt.to_pydatetime() accessor on a datetime-typed Series:
import pandas as pd
df = pd.DataFrame({
'event': ['Login', 'Purchase', 'Logout'],
'timestamp': pd.to_datetime([
'2024-01-01 12:00:00',
'2024-01-02 14:30:00',
'2024-01-03 08:45:00'
])
})
# Convert the entire column to Python datetime objects
datetime_array = df['timestamp'].dt.to_pydatetime()
print("Type of result:", type(datetime_array))
print("Type of element:", type(datetime_array[0]))
print(datetime_array)
Output:
Type of result: <class 'numpy.ndarray'>
Type of element: <class 'datetime.datetime'>
[datetime.datetime(2024, 1, 1, 12, 0)
datetime.datetime(2024, 1, 2, 14, 30)
datetime.datetime(2024, 1, 3, 8, 45)]
Converting a Column to Date Only
import pandas as pd
df = pd.DataFrame({
'event': ['Login', 'Purchase', 'Logout'],
'timestamp': pd.to_datetime([
'2024-01-01 12:00:00',
'2024-01-02 14:30:00',
'2024-01-03 08:45:00'
])
})
# Extract date only from each timestamp
df['date'] = df['timestamp'].dt.date
print(df)
print("\nType of date value:", type(df['date'][0]))
Output:
event timestamp date
0 Login 2024-01-01 12:00:00 2024-01-01
1 Purchase 2024-01-02 14:30:00 2024-01-02
2 Logout 2024-01-03 08:45:00 2024-01-03
Type of date value: <class 'datetime.date'>
Method 4: Using pd.to_datetime() to Create Timestamps from Strings
Before converting Timestamps to datetime, you often need to create them first. pd.to_datetime() parses strings into Pandas Timestamps:
import pandas as pd
# Convert string to Timestamp
ts = pd.to_datetime("2024-03-15 09:30:00")
print("Type:", type(ts))
print("Value:", ts)
# Then convert to Python datetime
dt = ts.to_pydatetime()
print("Datetime type:", type(dt))
Output:
Type: <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Value: 2024-03-15 09:30:00
Datetime type: <class 'datetime.datetime'>
Handling Multiple Formats
pd.to_datetime() handles various date string formats:
import pandas as pd
dates = ['01/15/2024', '2024-02-20', 'March 5, 2024', '2024.04.10']
for date_str in dates:
ts = pd.to_datetime(date_str)
print(f"{date_str:20s} → {ts}")
Output:
01/15/2024 → 2024-01-15 00:00:00
2024-02-20 → 2024-02-20 00:00:00
March 5, 2024 → 2024-03-05 00:00:00
2024.04.10 → 2024-04-10 00:00:00
Common Mistake: Using datetime.date When datetime.datetime Is Expected
A frequent error is using the date() method when the downstream code expects a full datetime object with time information:
import pandas as pd
ts = pd.Timestamp("2024-06-15 14:30:00")
# WRONG: date() loses the time component
date_obj = ts.date()
print(f"Date only: {date_obj}")
# This fails if you try to access time attributes
try:
print(date_obj.hour)
except AttributeError as e:
print(f"Error: {e}")
Output:
Date only: 2024-06-15
Error: 'datetime.date' object has no attribute 'hour'
The correct approach when you need both date and time:
import pandas as pd
ts = pd.Timestamp("2024-06-15 14:30:00")
# CORRECT: use to_pydatetime() to preserve time information
datetime_obj = ts.to_pydatetime()
print(f"Full datetime: {datetime_obj}")
print(f"Hour: {datetime_obj.hour}")
Output:
Full datetime: 2024-06-15 14:30:00
Hour: 14
Use date() only when you explicitly want to discard the time portion. For most conversions, to_pydatetime() is the correct choice since it preserves all components of the timestamp.
Extracting Individual Components
Sometimes you don't need a full conversion - you just need specific parts of the timestamp:
import pandas as pd
ts = pd.Timestamp("2024-07-20 16:45:30.123456")
print(f"Year: {ts.year}")
print(f"Month: {ts.month}")
print(f"Day: {ts.day}")
print(f"Hour: {ts.hour}")
print(f"Minute: {ts.minute}")
print(f"Second: {ts.second}")
print(f"Microsecond: {ts.microsecond}")
print(f"Day of week: {ts.day_name()}")
print(f"Quarter: {ts.quarter}")
Output:
Year: 2024
Month: 7
Day: 20
Hour: 16
Minute: 45
Second: 30
Microsecond: 123456
Day of week: Saturday
Quarter: 3
Quick Reference
| Method | Returns | Preserves Time | Preserves Timezone | Use When |
|---|---|---|---|---|
ts.to_pydatetime() | datetime.datetime | Yes | Yes | You need a full Python datetime object |
ts.date() | datetime.date | No | No | You only need the date portion |
ts.time() | datetime.time | N/A (time only) | No | You only need the time portion |
df['col'].dt.to_pydatetime() | ndarray of datetime | Yes | Yes | Converting an entire column |
df['col'].dt.date | Series of datetime.date | No | No | Extracting dates from a column |
- For most use cases,
to_pydatetime()is the safest and most complete conversion method. - It preserves all timestamp information and produces a standard Python
datetimeobject that works seamlessly with any library expecting native datetime types.