You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling pd.PeriodIndex constructor on pd.Index cannot infer frequency, while first calling pd.DateTimeIndex on pd.Index and calling .to_period() does work #54869
I have the following pd.Index: Index(['2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01', '2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01', '2022-09-01', '2022-10-01', '2022-11-01', '2022-12-01'], dtype='object', name='period')
I want to transform this index to a pd.PeriodIndex. I tried this using the pd.PeriodIndex constructor, but this give the following error: {ValueError}freq not specified and cannot be inferred.
However, if I first call pd.DateTimeIndex on the index and then call the .to_period() method on the resulting DateTimeIndex, the result is as expected: PeriodIndex(['2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06', '2021-07', '2021-08', '2021-09', '2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'], dtype='period[M]', name='period').
This does not make sense to me: apparently the frequency CAN be inferred form the original index, but the pd.PeriodIndex constructor is not equipped for this.