Pandas - Convert a Timestamp object to a native Python datetime object

PythonServer Side ProgrammingProgrammingPandas

To convert a Timestamp object to a native Python datetime object, use the timestamp.to_pydatetime() method.

At first, import the required libraries −

import pandas as pd

Create the timestamp object in Pandas

timestamp = pd.Timestamp('2021-09-11T13:12:34.261811')

Convert timestamp to native Python datetime object

timestamp.to_pydatetime()

Example

Following is the code

import pandas as pd

# set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-11T13:12:34.261811')

# display the Timestamp
print("Timestamp...\n", timestamp)

# convert timestamp to native Python datetime object
print("\nConvert Timestamp...\n", timestamp.to_pydatetime())

Output

This will produce the following code

Timestamp...
 2021-09-11 13:12:34.261811
Convert Timestamp...
 2021-09-11 13:12:34.261811
raja
Updated on 13-Oct-2021 07:06:38

Advertisements