Skip to content

Commit 67055d5

Browse files
authored
CLN: remove from PeriodIndex.asfreq unnecessary raising ValueError for invalid period freq (#57300)
remove from asfreq unnecessary raising ValueError, correct tests
1 parent 65b267a commit 67055d5

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

pandas/core/resample.py

-5
Original file line numberDiff line numberDiff line change
@@ -2773,11 +2773,6 @@ def asfreq(
27732773
if isinstance(freq, BaseOffset):
27742774
if hasattr(freq, "_period_dtype_code"):
27752775
freq = freq_to_period_freqstr(freq.n, freq.name)
2776-
else:
2777-
raise ValueError(
2778-
f"Invalid offset: '{freq.base}' for converting time series "
2779-
f"with PeriodIndex."
2780-
)
27812776

27822777
new_obj = obj.copy()
27832778
new_obj.index = obj.index.asfreq(freq, how=how)

pandas/tests/indexes/period/methods/test_asfreq.py

+12-22
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,21 @@ def test_asfreq_with_different_n(self):
142142
tm.assert_series_equal(result, excepted)
143143

144144
@pytest.mark.parametrize(
145-
"freq",
145+
"freq, is_str",
146146
[
147-
"2BMS",
148-
"2YS-MAR",
149-
"2bh",
147+
("2BMS", True),
148+
("2YS-MAR", True),
149+
("2bh", True),
150+
(offsets.MonthBegin(2), False),
151+
(offsets.BusinessMonthEnd(2), False),
150152
],
151153
)
152-
def test_pi_asfreq_not_supported_frequency(self, freq):
153-
# GH#55785
154-
msg = f"{freq[1:]} is not supported as period frequency"
154+
def test_pi_asfreq_not_supported_frequency(self, freq, is_str):
155+
# GH#55785, GH#56945
156+
if is_str:
157+
msg = f"{freq[1:]} is not supported as period frequency"
158+
else:
159+
msg = re.escape(f"{freq} is not supported as period frequency")
155160

156161
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
157162
with pytest.raises(ValueError, match=msg):
@@ -172,18 +177,3 @@ def test_pi_asfreq_invalid_frequency(self, freq):
172177
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
173178
with pytest.raises(ValueError, match=msg):
174179
pi.asfreq(freq=freq)
175-
176-
@pytest.mark.parametrize(
177-
"freq",
178-
[
179-
offsets.MonthBegin(2),
180-
offsets.BusinessMonthEnd(2),
181-
],
182-
)
183-
def test_pi_asfreq_invalid_baseoffset(self, freq):
184-
# GH#56945
185-
msg = re.escape(f"{freq} is not supported as period frequency")
186-
187-
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
188-
with pytest.raises(ValueError, match=msg):
189-
pi.asfreq(freq=freq)

pandas/tests/resample/test_period_index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import re
23
import warnings
34

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

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

0 commit comments

Comments
 (0)