Skip to content
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
5 changes: 0 additions & 5 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2773,11 +2773,6 @@ def asfreq(
if isinstance(freq, BaseOffset):
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
34 changes: 12 additions & 22 deletions pandas/tests/indexes/period/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,21 @@ def test_asfreq_with_different_n(self):
tm.assert_series_equal(result, excepted)

@pytest.mark.parametrize(
"freq",
"freq, is_str",
[
"2BMS",
"2YS-MAR",
"2bh",
("2BMS", True),
("2YS-MAR", True),
("2bh", True),
(offsets.MonthBegin(2), False),
(offsets.BusinessMonthEnd(2), False),
],
)
def test_pi_asfreq_not_supported_frequency(self, freq):
# GH#55785
msg = f"{freq[1:]} is not supported as period frequency"
def test_pi_asfreq_not_supported_frequency(self, freq, is_str):
# GH#55785, GH#56945
if is_str:
msg = f"{freq[1:]} is not supported as period frequency"
else:
msg = re.escape(f"{freq} is not supported as period frequency")

pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
with pytest.raises(ValueError, match=msg):
Expand All @@ -172,18 +177,3 @@ def test_pi_asfreq_invalid_frequency(self, freq):
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
with pytest.raises(ValueError, match=msg):
pi.asfreq(freq=freq)

@pytest.mark.parametrize(
"freq",
[
offsets.MonthBegin(2),
offsets.BusinessMonthEnd(2),
],
)
def test_pi_asfreq_invalid_baseoffset(self, freq):
# GH#56945
msg = re.escape(f"{freq} is not supported as period frequency")

pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
with pytest.raises(ValueError, match=msg):
pi.asfreq(freq=freq)
3 changes: 2 additions & 1 deletion pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import re
import warnings

import dateutil
Expand Down Expand Up @@ -1034,7 +1035,7 @@ def test_resample_lowercase_frequency_deprecated(
)
def test_asfreq_invalid_period_offset(self, offset, frame_or_series):
# GH#55785
msg = f"Invalid offset: '{offset.base}' for converting time series "
msg = re.escape(f"{offset} is not supported as period frequency")

obj = frame_or_series(range(5), index=period_range("2020-01-01", periods=5))
with pytest.raises(ValueError, match=msg):
Expand Down