Skip to content

Commit 5f2abaa

Browse files
authored
DEPR: raise ValueError if invalid period freq pass to asfreq (#56489)
* raise ValueError if invalid period freq pass to asfreq * fix pylint error
1 parent cbe1b32 commit 5f2abaa

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/resample.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,13 @@ def asfreq(
27842784
how = "E"
27852785

27862786
if isinstance(freq, BaseOffset):
2787-
freq = freq_to_period_freqstr(freq.n, freq.name)
2787+
if hasattr(freq, "_period_dtype_code"):
2788+
freq = freq_to_period_freqstr(freq.n, freq.name)
2789+
else:
2790+
raise ValueError(
2791+
f"Invalid offset: '{freq.base}' for converting time series "
2792+
f"with PeriodIndex."
2793+
)
27882794

27892795
new_obj = obj.copy()
27902796
new_obj.index = obj.index.asfreq(freq, how=how)

pandas/tests/resample/test_period_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,22 @@ def test_resample_t_l_deprecated(self):
970970
result = ser.resample("T").mean()
971971
tm.assert_series_equal(result, expected)
972972

973+
@pytest.mark.parametrize(
974+
"offset",
975+
[
976+
offsets.MonthBegin(),
977+
offsets.BYearBegin(2),
978+
offsets.BusinessHour(2),
979+
],
980+
)
981+
def test_asfreq_invalid_period_freq(self, offset, series_and_frame):
982+
# GH#9586
983+
msg = f"Invalid offset: '{offset.base}' for converting time series "
984+
985+
df = series_and_frame
986+
with pytest.raises(ValueError, match=msg):
987+
df.asfreq(freq=offset)
988+
973989

974990
@pytest.mark.parametrize(
975991
"freq,freq_depr",

0 commit comments

Comments
 (0)