Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Pandas DatetimeIndex.to_pydatetime()

Python | Pandas DatetimeIndex.to_pydatetime()

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 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 : ndarray

Example #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)


Output :

Now we want to convert the DatetimeIndex to datetime.datetime objects.




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


Output :

As we can see in the output, the function has converted the DatetimeIndex object to python datetime.datetime object.
 
Example #2: 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 'M' represents monthly frequency 
didx = pd.DatetimeIndex(start ='2015-03-02', freq ='M', periods = 5)
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want to convert the DatetimeIndex to datetime.datetime objects.




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


Output :

As we can see in the output, the function has converted the DatetimeIndex object to python datetime.datetime object.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments