Python | Pandas DatetimeIndex.to_pydatetime()
Last Updated :
29 Dec, 2018
Improve
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
Python3
Output :
Now we want to convert the DatetimeIndex to datetime.datetime objects.
Python3
Output :
As we can see in the output, the function has converted the DatetimeIndex object to python datetime.datetime object.
Example #2: Use
Python3
Output :
Now we want to convert the DatetimeIndex to datetime.datetime objects.
Python3
Output :
As we can see in the output, the function has converted the DatetimeIndex object to python datetime.datetime object.
DatetimeIndex.to_pydatetime()
function return DatetimeIndex as object ndarray of datetime.datetime objects. The function does not take any input value.
Syntax: DatetimeIndex.to_pydatetime() Parameters : None Return : ndarrayExample #1: Use
DatetimeIndex.to_pydatetime()
function to convert the DatetimeIndex as object ndarray of datetime.datetime objects.
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'S' represents secondly frequency
didx = pd.DatetimeIndex(start ='2018-11-15 09:45:10', freq ='S', periods = 5)
# Print the DatetimeIndex
print(didx)

# convert to datetime.datetime objects.
didx.to_pydatetime()

DatetimeIndex.to_pydatetime()
function to convert the DatetimeIndex as object ndarray of datetime.datetime objects.
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'M' represents monthly frequency
didx = pd.DatetimeIndex(start ='2015-03-02', freq ='M', periods = 5)
# Print the DatetimeIndex
print(didx)

# convert to datetime.datetime objects.
didx.to_pydatetime()
