Open In App

Python | Pandas PeriodIndex.to_timestamp

Last Updated : 06 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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. Python3
# importing pandas as pd
import pandas as pd

# Create the PeriodIndex object
pidx = pd.PeriodIndex(start = '2004-11-11 02:45:21 ',
               end = '2021-5-21 8:45:29', freq = 'Y')

# Print the PeriodIndex object
print(pidx)
Output : Now we will use the PeriodIndex.to_timestamp() function to cast the given PeriodIndex object to DatetimeIndex object. Python3
# cast to DatetimeIndex object
pidx.to_timestamp()
] 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. Python3
# importing pandas as pd
import pandas as pd

# Create the PeriodIndex object
pidx = pd.PeriodIndex(start = '2016-10-12 11:12:02', 
            end = '2016-10-12 11:19:12', freq = 'T')

# Print the PeriodIndex object
print(pidx)
Output : Now we will use the PeriodIndex.to_timestamp() function to cast the given PeriodIndex object to DatetimeIndex object. Python3
# cast to DatetimeIndex object
pidx.to_timestamp()
] Output : As we can see in the output, the PeriodIndex.to_timestamp() function has returned a DatetimeIndex object.

Next Article

Similar Reads