Python | Pandas PeriodIndex.to_timestamp
Last Updated :
06 Jan, 2019
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 PeriodIndex.to_timestamp()
function casts the given PeriodIndex object to DatetimeIndex object.
Syntax : PeriodIndex.to_timestamp(freq=None, how=’start’)
Parameters :
freq : string or DateOffset, default ‘D’ for week or longer, ‘S’
how : {‘s’, ‘e’, ‘start’, ‘end’}
Return : DatetimeIndex
Example #1: Use PeriodIndex.to_timestamp()
function to cast the given PeriodIndex object to DatetimeIndex.
import pandas as pd
pidx = pd.PeriodIndex(start = '2004-11-11 02:45:21 ' ,
end = '2021-5-21 8:45:29' , freq = 'Y' )
print (pidx)
|
Output :
Now we will use the PeriodIndex.to_timestamp()
function to cast the given PeriodIndex object to DatetimeIndex object.
Output :
As we can see in the output, the PeriodIndex.to_timestamp()
function has returned a DatetimeIndex object.
Example #2: Use PeriodIndex.to_timestamp()
function to cast the given PeriodIndex object to DatetimeIndex.
import pandas as pd
pidx = pd.PeriodIndex(start = '2016-10-12 11:12:02' ,
end = '2016-10-12 11:19:12' , freq = 'T' )
print (pidx)
|
Output :
Now we will use the PeriodIndex.to_timestamp()
function to cast the given PeriodIndex object to DatetimeIndex object.
Output :
As we can see in the output, the PeriodIndex.to_timestamp()
function has returned a DatetimeIndex object.
Share your thoughts in the comments
Please Login to comment...