The to_pydatetime()
function in pandas converts a Timestamp
object to the equivalent Python object, datetime
.
We can use to_pydatetime()
function with the syntax below:
Timestamp.to_pydatetime()
The to_pydatetime()
function takes no parameter value other than the Timestamp
object, which is to be converted to datetime
.
The to_pydatetime()
function returns a datetime
object that returns the same date and time value from the input Timestamp
object.
# A code to illustrate the to_pydatetime() function in Pandas# importing the pandas libraryimport pandas as pd# creating a Timestamp objectmy_timestamp = pd.Timestamp("2022-05-01T10:30:52.192548")# printing the Timestamp objectprint(my_timestamp)# converting the Timestamp to python's datetime objectmy_datetime = my_timestamp.to_pydatetime()# printing the datetime objectprint(my_datetime)# checking the object typesprint(type(my_timestamp))print(type(my_datetime))
pandas
library.Timestamp
object, my_timestamp
.Timestamp
object, my_timestamp
.Timestamp
object to a datetime
object using the to_pydatetime()
function and store the result in my_datetime
.datetime
object, my_datetime
.type()
function to check for the object types of my_timestamp
and my_datetime
. We can see that they have different types but contain the same value of date and time.