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

DEPR: raise ValueError if invalid period freq pass to asfreq #56489

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2784,7 +2784,13 @@ def asfreq(
how = "E"

if isinstance(freq, BaseOffset):
freq = freq_to_period_freqstr(freq.n, freq.name)
if hasattr(freq, "_period_dtype_code"):
freq = freq_to_period_freqstr(freq.n, freq.name)
else:
raise ValueError(
f"Invalid offset: '{freq.base}' for converting time series "
f"with PeriodIndex."
)

new_obj = obj.copy()
new_obj.index = obj.index.asfreq(freq, how=how)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,22 @@ def test_resample_t_l_deprecated(self):
result = ser.resample("T").mean()
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"offset",
[
offsets.MonthBegin(),
offsets.BYearBegin(2),
offsets.BusinessHour(2),
],
)
def test_asfreq_invalid_period_freq(self, offset, series_and_frame):
# GH#9586
msg = f"Invalid offset: '{offset.base}' for converting time series "

df = series_and_frame
with pytest.raises(ValueError, match=msg):
df.asfreq(freq=offset)


@pytest.mark.parametrize(
"freq,freq_depr",
Expand Down