Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: raise ValueError if invalid period freq pass to asfreq when the index of df is a PeriodIndex #56945

Merged
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4846,7 +4846,7 @@ cpdef to_offset(freq, bint is_period=False):
)

elif PyDelta_Check(freq):
return delta_to_tick(freq)
delta = delta_to_tick(freq)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe on line 4841, we can do

    if isinstance(freq, BaseOffset):
        delta = freq

so then that gets validated too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to do this assignment on line 4841 as well? just in case an offset which isn't valid for periods is passed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure if it's possible. I tried to do this assignment on line 4841

if isinstance(freq, BaseOffset):
    delta = freq

but then I got failures.
The reason: if we replace return freq with the delta = freq, we go to the line 4962 and assign delta to None and then on line 4965 we raise a ValueError.
Which is why instead of the assignment delta = freq I added the check

if is_period and not hasattr(freq, "_period_dtype_code"):
    raise ValueError(f"{freq.base} is not supported as period frequency")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just move this down to before elif PyDelta_Check(freq):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I saw you already made commit for it


elif isinstance(freq, str):
delta = None
Expand Down Expand Up @@ -4964,6 +4964,9 @@ cpdef to_offset(freq, bint is_period=False):
if delta is None:
raise ValueError(INVALID_FREQ_ERR_MSG.format(freq))

if is_period and not hasattr(delta, "_period_dtype_code"):
raise ValueError(INVALID_FREQ_ERR_MSG.format(freq))

return delta


Expand Down
12 changes: 0 additions & 12 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,6 @@ def _resolution_obj(self) -> Resolution:
**_shared_doc_kwargs,
)
def asfreq(self, freq=None, how: str = "E") -> Self:
if isinstance(freq, str):
offset = to_offset(freq, is_period=True)
if hasattr(offset, "_period_dtype_code"):
freq = freq_to_period_freqstr(offset.n, offset.name)
elif offset.name == freq.replace(f"{offset.n}", ""):
raise ValueError(
f"Invalid offset: '{offset.name}' for converting time series "
f"with PeriodIndex."
)
else:
raise ValueError(INVALID_FREQ_ERR_MSG.format(f"{freq}"))

arr = self._data.asfreq(freq, how)
Copy link
Member

@MarcoGorelli MarcoGorelli Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you follow this asfreq and put this validation even deeper?

EDIT: as mentioned in the review, it might be better to go all the way down and do this validation within to_offset itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I did as you suggested and moved this validation to to_offset. It works very well.

return type(self)._simple_new(arr, name=self.name)

Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/indexes/period/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,6 @@ def test_asfreq_with_different_n(self):
"2BMS",
"2YS-MAR",
"2bh",
],
)
def test_pi_asfreq_invalid_offset(self, freq):
# GH#55785
msg = f"Invalid offset: '{freq[1:]}' for converting time series "

pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
with pytest.raises(ValueError, match=msg):
pi.asfreq(freq=freq)

@pytest.mark.parametrize(
"freq",
[
"2BME",
"2YE-MAR",
"2BM",
Expand Down
Loading