From 06852a4fd326417fbe57807dd85a4fe4ceea59fd Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 7 Feb 2024 22:51:23 +0100 Subject: [PATCH] remove from asfreq unnecessary raising ValueError, correct tests --- pandas/core/resample.py | 5 --- .../indexes/period/methods/test_asfreq.py | 34 +++++++------------ pandas/tests/resample/test_period_index.py | 3 +- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 2a36c0f1ef549..63ea60fe2763b 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -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) diff --git a/pandas/tests/indexes/period/methods/test_asfreq.py b/pandas/tests/indexes/period/methods/test_asfreq.py index 865bae69d91c7..ea305a9766103 100644 --- a/pandas/tests/indexes/period/methods/test_asfreq.py +++ b/pandas/tests/indexes/period/methods/test_asfreq.py @@ -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): @@ -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) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 610eeb6dcf32f..6fdc398b13835 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -1,4 +1,5 @@ from datetime import datetime +import re import warnings import dateutil @@ -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):