pandas.PeriodIndex.to_timestamp#

PeriodIndex.to_timestamp(freq=None, how='start')[source]#

Cast to DatetimeArray/Index.

Parameters:
freqstr or DateOffset, optional

Target frequency. The default is ‘D’ for week or longer, ‘s’ otherwise.

how{‘s’, ‘e’, ‘start’, ‘end’}

Whether to use the start or end of the time period being converted.

Returns:
DatetimeArray/Index

Examples

>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
>>> idx.to_timestamp()
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'],
dtype='datetime64[ns]', freq='MS')

The frequency will not be inferred if the index contains less than three elements, or if the values of index are not strictly monotonic:

>>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M")
>>> idx.to_timestamp()
DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[ns]', freq=None)
>>> idx = pd.PeriodIndex(
...     ["2023-01", "2023-02", "2023-02", "2023-03"], freq="2M"
... )
>>> idx.to_timestamp()
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-02-01', '2023-03-01'],
dtype='datetime64[ns]', freq=None)