From 74edf285efd4a267f252f04ad1ff83424e67895d Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 23 Nov 2023 10:53:41 +0100 Subject: [PATCH 1/2] correct warning message [skip ci] --- pandas/_libs/tslibs/offsets.pyx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 01962c47ff31c..ef189becc77cf 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -58,7 +58,7 @@ from pandas._libs.tslibs.conversion cimport localize_pydatetime from pandas._libs.tslibs.dtypes cimport ( c_DEPR_ABBREVS, c_OFFSET_DEPR_FREQSTR, - c_REVERSE_OFFSET_DEPR_FREQSTR, + c_OFFSET_TO_PERIOD_FREQSTR, periods_per_day, ) from pandas._libs.tslibs.nattype cimport ( @@ -4729,16 +4729,17 @@ cpdef to_offset(freq, bint is_period=False): stacklevel=find_stack_level(), ) name = c_OFFSET_DEPR_FREQSTR[name] - if is_period is True and name in c_REVERSE_OFFSET_DEPR_FREQSTR: + if is_period is True and name in c_OFFSET_TO_PERIOD_FREQSTR: if name.startswith("Y"): raise ValueError( - f"for Period, please use \'Y{name[2:]}\' " + f"for Period, please use " + f"\'Y{c_OFFSET_TO_PERIOD_FREQSTR.get(name)[2:]}\' " f"instead of \'{name}\'" ) else: raise ValueError( f"for Period, please use " - f"\'{c_REVERSE_OFFSET_DEPR_FREQSTR.get(name)}\' " + f"\'{c_OFFSET_TO_PERIOD_FREQSTR.get(name)}\' " f"instead of \'{name}\'" ) elif is_period is True and name in c_OFFSET_DEPR_FREQSTR: From 39fbd1a50cba7aedbe0c303a31c9645e0cfdca60 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 23 Nov 2023 23:49:00 +0100 Subject: [PATCH 2/2] DEPR: correct error message in to_offset --- pandas/_libs/tslibs/offsets.pyx | 12 +++++++----- pandas/tests/indexes/period/test_period.py | 12 ++++++------ pandas/tests/resample/test_period_index.py | 22 +++++++++++++++++++--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index ef189becc77cf..445330e813d2c 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -58,7 +58,7 @@ from pandas._libs.tslibs.conversion cimport localize_pydatetime from pandas._libs.tslibs.dtypes cimport ( c_DEPR_ABBREVS, c_OFFSET_DEPR_FREQSTR, - c_OFFSET_TO_PERIOD_FREQSTR, + c_REVERSE_OFFSET_DEPR_FREQSTR, periods_per_day, ) from pandas._libs.tslibs.nattype cimport ( @@ -4729,17 +4729,19 @@ cpdef to_offset(freq, bint is_period=False): stacklevel=find_stack_level(), ) name = c_OFFSET_DEPR_FREQSTR[name] - if is_period is True and name in c_OFFSET_TO_PERIOD_FREQSTR: + if is_period is True and name in c_REVERSE_OFFSET_DEPR_FREQSTR: if name.startswith("Y"): raise ValueError( - f"for Period, please use " - f"\'Y{c_OFFSET_TO_PERIOD_FREQSTR.get(name)[2:]}\' " + f"for Period, please use \'Y{name[2:]}\' " f"instead of \'{name}\'" ) + if (name.startswith("B") or + name.startswith("S") or name.startswith("C")): + raise ValueError(INVALID_FREQ_ERR_MSG.format(name)) else: raise ValueError( f"for Period, please use " - f"\'{c_OFFSET_TO_PERIOD_FREQSTR.get(name)}\' " + f"\'{c_REVERSE_OFFSET_DEPR_FREQSTR.get(name)}\' " f"instead of \'{name}\'" ) elif is_period is True and name in c_OFFSET_DEPR_FREQSTR: diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 7b7baab112264..1354f33291c45 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -285,10 +285,11 @@ def test_map(self): "freq,freq_depr", [ ("2M", "2ME"), - ("2SM", "2SME"), + ("2Q-MAR", "2QE-MAR"), + ("2Y-FEB", "2YE-FEB"), ], ) - def test_period_index_frequency_ME_SME_error_message(self, freq, freq_depr): + def test_period_index_frequency_ME_error_message(self, freq, freq_depr): # GH#52064 msg = f"for Period, please use '{freq[1:]}' instead of '{freq_depr[1:]}'" @@ -316,11 +317,10 @@ def test_a_deprecated_from_time_series(self, freq_depr): series = Series(1, index=index) assert isinstance(series, Series) - @pytest.mark.parametrize("freq_depr", ["2ME", "2QE", "2BQE", "2YE"]) - def test_period_index_frequency_error_message(self, freq_depr): + @pytest.mark.parametrize("freq_depr", ["2SME", "2CBME", "2BYE"]) + def test_period_index_frequency_invalid_freq(self, freq_depr): # GH#9586 - msg = f"for Period, please use '{freq_depr[1:-1]}' " - f"instead of '{freq_depr[1:]}'" + msg = f"Invalid frequency: {freq_depr[1:]}" with pytest.raises(ValueError, match=msg): period_range("2020-01", "2020-05", freq=freq_depr) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 864ad4605b54d..b3d346eb22ac5 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -956,11 +956,8 @@ def test_resample_t_l_deprecated(self): ("2M", "2ME"), ("2Q", "2QE"), ("2Q-FEB", "2QE-FEB"), - ("2BQ", "2BQE"), - ("2BQ-FEB", "2BQE-FEB"), ("2Y", "2YE"), ("2Y-MAR", "2YE-MAR"), - ("2BA-MAR", "2BYE-MAR"), ], ) def test_resample_frequency_ME_QE_YE_error_message(series_and_frame, freq, freq_depr): @@ -978,3 +975,22 @@ def test_corner_cases_period(simple_period_range_series): # it works result = len0pts.resample("Y-DEC").mean() assert len(result) == 0 + + +@pytest.mark.parametrize( + "freq_depr", + [ + "2BME", + "2CBME", + "2SME", + "2BQE-FEB", + "2BYE-MAR", + ], +) +def test_resample_frequency_invalid_freq(series_and_frame, freq_depr): + # GH#9586 + msg = f"Invalid frequency: {freq_depr[1:]}" + + obj = series_and_frame + with pytest.raises(ValueError, match=msg): + obj.resample(freq_depr)