• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Convert Pandas Timestamp to Datetime

How do you convert Pandas timestamp to Python datetime? – In Pandas, a Timestamp is a specific type of object representing a single timestamp, and it is a subtype of datetime. Therefore, if you have a Pandas Timestamp object and you want to convert it to a regular Python datetime object, you can do so using the to_pydatetime() method. In this article, I will explain how to convert Pandas timestamp to datetime with examples.

Key Points

  • Timestamp is the Pandas data structure for representing datetime information. It’s an extension of Python’s datetime class and provides additional functionality.
  • The datetime module in Python provides classes for working with dates and times. The main class is datetime.datetime, which represents a point in time and includes both date and time information.
  • The to_pydatetime() method is used to convert Pandas Timestamp objects to Python’s datetime.datetime objects.
  • You can convert other datetime-like objects, such as Python’s datetime or NumPy’s datetime64, to Timestamp objects using the pd.to_datetime() function.
  • If you have missing or undefined datetime values represented as NaT (Not a Time) in your Timestamps, the to_pydatetime() method will handle these values gracefully, converting them to the datetime.datetime equivalent with a None representation.

Syntax of Pandas to_pydatetime()

Following is the syntax of the to_pydatetime().


# Syntax of to_pydatetime()
pandas_timestamp.to_pydatetime()

Here,

  • pandas_timestamp – It refers to the Pandas Timestamp object that you want to convert to a Python datetime object.
  • to_pydatetime() – This function takes no parameter value other than the Timestamp object, which is to be converted to datetime.

Return value

It returns a datetime object that returns the same date and time value from the input Timestamp object. If you have missing or undefined datetime values represented as NaT (Not a Time) in your Timestamps, the to_pydatetime() method will handle these values gracefully, converting them to the datetime.datetime equivalent with a None representation.

Convert a Pandas Timestamp to a Datetime

You can use the Pandas to_pydatetime() method to convert Pandas Timestamp to regular Python datetime object. Timestamp is the Pandas data structure for representing datetime information. It’s an extension of Python’s datetime class and provides additional functionality. The datetime module in Python provides classes for working with dates and times. The main class is datetime.datetime, which represents a point in time and includes both date and time information.

Remember that you can perform various operations directly with Pandas Timestamps in many cases without explicitly converting them to Python datetime objects. The conversion may be necessary if you need to use libraries or functions that specifically expect standard Python datetime objects.


# Create DataFrame
import pandas as pd

# Create a pandas timestamp
pandas_timestamp = pd.Timestamp('2024-01-17 00:00:00')

# Convert Pandas Timestamp to Python datetime
python_datetime = pandas_timestamp.to_pydatetime()
print("Pandas Timestamp:", pandas_timestamp)
print("Python Datetime:", python_datetime)

Yields below output. Here, we created a Pandas Timestamp (pandas_timestamp) and then converts it to a Python datetime object (python_datetime) using the to_pydatetime() method.

pandas convert timestamp datetime

Alternatively, use pd.date_range() to generate a DatetimeIndex and then apply the result to to_pydatetime() method to convert it to a NumPy array of Python datetime objects.

The following program generates a DatetimeIndex with timestamps every hour starting from ‘2024-01-17 12:00:00’. Then, the to_pydatetime() method is applied to convert this DatetimeIndex to a NumPy array of Python datetime objects.


import pandas as pd

# Convert the DatetimeIndex to an array of datetimes
stamps = pd.date_range(start='2024-01-17 12:00:00', periods=6, freq='H')
datetimes_array = stamps.to_pydatetime()
print("Convert timestamps to datetimes:\n", datetimes_array)

Yields below output.

pandas convert timestamp datetime

Convert a Pandas Column of Timestamps to Datetimes

If you have a Pandas DataFrame with a column containing Timestamps, and you want to convert that entire column to a column of Python datetimes, you can use the apply function along with the to_pydatetime() method.

In the below example, a new column named datetime_column is created by applying a lambda function to each element in the timestamp_column. The lambda function converts each Timestamp to a Python datetime using the to_pydatetime() method.


import pandas as pd

# Create a sample DataFrame
data = {'timestamp_column': ['2024-01-17 12:34:56', '2022-05-21 09:45:30', '2023-08-10 18:20:15']}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df[['timestamp_column']])

# Convert the Pandas timestamp column to datetime column
df['datetime_column'] = df['timestamp_column'].apply(lambda x: pd.Timestamp(x).to_pydatetime())
print("\nDataFrame with Converted Datetime Column:")
print(df[['datetime_column']])

# Output:
# Original DataFrame:
#       timestamp_column
# 0  2024-01-17 12:34:56
# 1  2022-05-21 09:45:30
# 2  2023-08-10 18:20:15

# DataFrame with Converted Datetime Column:
#       datetime_column
# 0 2024-01-17 12:34:56
# 1 2022-05-21 09:45:30
# 2 2023-08-10 18:20:15

Similarly, follow the below example to convert a pandas column of timestamps to datetimes. To apply a lambda function to each element in the timestamp_column. The lambda function (lambda x: x.date()) extracts the date part from each timestamp using the date() method. As a result, the timestamp_column is modified, and it now contains Python date objects instead of Pandas Timestamp objects.


import pandas as pd

# Create DataFrame
df = pd.DataFrame({'timestamp_column': pd.date_range(start='2024-01-17 12:00:00',
                             periods=6,
                             freq='H'),
                   'sales': [12, 15, 23, 28, 41, 35]})
                     
# Convert column of timestamps to datetimes
df.timestamp_column = df.timestamp_column.apply(lambda x: x.date())
print(df)

# Output:
#   timestamp_column  sales
# 0       2024-01-17     12
# 1       2024-01-17     15
# 2       2024-01-17     23
# 3       2024-01-17     28
# 4       2024-01-17     41
# 5       2024-01-17     35

Convert an Array of Timestamps to Datetimes

If you have an array of Timestamps and you want to convert them to a NumPy array of Python datetime objects, you can use the to_pydatetime() method for each timestamp in the array.

In the below example, timestamps_array is an array of Pandas Timestamps, and datetimes_array is created by applying the to_pydatetime() method to each element in timestamps_array using a list comprehension. The resulting array datetimes_array contains Python datetime objects.


import pandas as pd
import numpy as np

# Create an array of Pandas Timestamps
timestamps_array = pd.to_datetime(['2024-01-17 12:34:56', '2023-08-10 18:20:15', '2022-05-21 09:45:30'])
print("Pandas Timestamps array:")
print(timestamps_array)

# Convert the array of Pandas Timestamps to an array of Python datetimes
datetimes_array = np.array([timestamp.to_pydatetime() for timestamp in timestamps_array])
print("\nPython Datetimes array:")
print(datetimes_array)

# Output:
# Pandas Timestamps array:
# DatetimeIndex(['2024-01-17 12:34:56', '2023-08-10 18:20:15',
#                '2022-05-21 09:45:30'],
#               dtype='datetime64[ns]', freq=None)

# Python Datetimes array:
# [datetime.datetime(2024, 1, 17, 12, 34, 56)
#  datetime.datetime(2023, 8, 10, 18, 20, 15)
#  datetime.datetime(2022, 5, 21, 9, 45, 30)]

Frequently Asked Questions on Convert Timestamp to Datetime

Why convert Pandas Timestamp to Python datetime?

Converting Pandas Timestamps to Python datetime objects can be useful when interacting with libraries or functions that specifically expect standard Python datetime objects. While Pandas Timestamps are datetime-like objects, some external libraries may not fully support them, and using Python datetime objects ensures compatibility.

Can I perform operations directly on Timestamps without converting to Python datetime?

In many cases, you can perform various operations directly on Timestamps without explicitly converting them to Python datetime objects. Pandas is built on top of NumPy, and many operations are optimized for Pandas Timestamps.

What is the difference between Pandas Timestamp and Python datetime?

While both represent points in time, Pandas Timestamp is a specific Pandas data type designed for timestamp values, while Python datetime is a standard Python module for working with dates and times. Pandas Timestamp is more tailored for handling time-series data within the Pandas ecosystem.

How to convert a Pandas DataFrame column of Timestamps to Python datetimes?

To convert a Pandas DataFrame column of Timestamps to Python datetimes, you can use the pd.to_datetime() function, which allows you to convert Pandas date-like objects to datetime-like objects.

Are there alternative ways to convert Timestamps to Python datetime?

Besides to_pydatetime(), you can also use the dt accessor to access datetime properties and create a new column with pd.to_datetime().

Conclusion

In this article, I have explained a comprehensive guide on converting Pandas Timestamps to Python datetime objects using the to_pydatetime() method. It covered various scenarios, including converting single Timestamps, Pandas DataFrame columns of Timestamps, and arrays of Timestamps. The article explained the syntax and parameters of the to_pydatetime() method, offering quick examples to illustrate its usage.

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.