Skip to content

Conversation

natmokval
Copy link
Contributor

@natmokval natmokval commented Jan 25, 2024

xref #52064

PeriodIndex.asfreq silently converts for offsets such as offsets.MonthBegin(), offsets.BusinessMonthEnd(), etc. (with no attribute '_period_dtype_code') frequency to period frequency (in this case 'M').

Reproducible Example:

>>> import pandas as pd
>>> from pandas.tseries import offsets
>>>
>>> index = pd.PeriodIndex(["2020-01-01", "2021-01-01"], freq="Q")
>>> index.asfreq(freq=offsets.MonthBegin(1))
PeriodIndex(['2020-03', '2021-03'], dtype='period[M]')
>>>
>>> index.asfreq(freq=offsets.BusinessMonthEnd(1)) 
PeriodIndex(['2020-03', '2021-03'], dtype='period[M]')

the correct behaviour would be raising an Error:

>>> index.asfreq(freq=offsets.MonthBegin(1))
ValueError: <MonthBegin> is not supported as period frequency
>>>
>>> index.asfreq(freq=offsets.BusinessMonthEnd(1))
ValueError: <BusinessMonthEnd> is not supported as period frequency

corrected the definition of PeriodIndex.asfreqand added tests.

@natmokval natmokval added Frequency DateOffsets Bug Deprecate Functionality to remove in pandas labels Jan 25, 2024
@natmokval natmokval marked this pull request as ready for review January 26, 2024 09:40
@natmokval
Copy link
Contributor Author

@MarcoGorelli, could you please take a look at this PR?

@MarcoGorelli
Copy link
Member

I'd like to think that the suggestion in #56945 (review) (i.e. raise if the return value of to_offset(..., is_period=True) doesn't have a _period_dtype_code attribute, could solve this too?

@natmokval
Copy link
Contributor Author

closed by #56945

@natmokval natmokval closed this Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Deprecate Functionality to remove in pandas Frequency DateOffsets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants