Coding Ref

How to convert Pandas timestamp to datetime

How to convert Pandas timestamp to datetime

To convert a Pandas timestamp to a datetime, you can use the to_pydatetime() method. This method will convert the timestamp to a native Python datetime object, which you can then use to work with the date and time data in the timestamp.

Here's an example of using the to_pydatetime() method to convert a Pandas timestamp to a datetime:

main.py
import pandas as pd

# create a sample timestamp
ts = pd.Timestamp("2022-01-01")

# convert the timestamp to a datetime
dt = ts.to_pydatetime()

# display the result
print(dt)

This will convert the timestamp to a native Python datetime object, and return the resulting datetime.

The output will be:

output
datetime.datetime(2022, 1, 1, 0, 0)

Conclusion

The to_pydatetime() method is a simple way to convert a Pandas timestamp to a datetime. It allows you to easily convert a timestamp to a native Python datetime object, which you can then use to work with the date and time data in the timestamp.

You'll also like

Related tutorials curated for you

    How to fix: AttributeError module 'pandas' has no attribute 'dataframe'

    How to convert string to float in Pandas

    What does Diff() do in Pandas?

    How to concatenate in Pandas

    How to calculate the variance in Pandas DataFrame

    How to stack two Pandas DataFrames

    What is .notnull in Pandas?

    How to get the number of columns in a Pandas DataFrame

    How to round in Pandas

    How to use pandas map() function

    fillna() in Pandas

    What is Pandas Cumsum()?