Convert a Period object to Datetime

Viewed 332

I would like to convert an object from pandas.core.indexes.accessors.PeriodProperties to a Datetime variable.

As an example, I want to convert a period variable (not a string) with the format "2019Q1" to a datetime format, "2019-03-31".

1 Answers

Assuming you have something like pi as in

pi = pd.PeriodIndex(["2019Q1", "2019Q2"], freq='Q')

then you want

pi.to_timestamp() + pd.offsets.QuarterEnd(0)

Out[11]: DatetimeIndex(['2019-03-31', '2019-06-30'], dtype='datetime64[ns]', freq=None)

It is basically described here.

Related