From 39b01ceb539d3ce7a77c7864b0172526bc009cad Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Sun, 7 Jan 2024 02:51:14 +0100 Subject: [PATCH 1/8] style --- pandas/core/arrays/period.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 90a691d1beb69..22f227abcfa68 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -732,11 +732,16 @@ def asfreq(self, freq=None, how: str = "E") -> Self: PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01', '2015-01'], dtype='period[M]') """ + freq_str = freq if isinstance(freq, str) else "" + how = libperiod.validate_end_alias(how) if isinstance(freq, BaseOffset): freq = freq_to_period_freqstr(freq.n, freq.name) freq = Period._maybe_convert_freq(freq) + if not hasattr(freq, "_period_dtype_code"): + raise TypeError(f'"{freq_str}" is not supported as a period frequency') + base1 = self._dtype._dtype_code base2 = freq._period_dtype_code From 1d4dee9a83e514c858347fd2752c5a08be802f73 Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Sun, 7 Jan 2024 03:30:58 +0100 Subject: [PATCH 2/8] add test --- pandas/tests/frame/methods/test_asfreq.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index 87d1745774487..63dc2eba6e470 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -8,6 +8,7 @@ from pandas import ( DataFrame, DatetimeIndex, + PeriodIndex, Series, date_range, period_range, @@ -257,3 +258,12 @@ def test_asfreq_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr): with tm.assert_produces_warning(FutureWarning, match=depr_msg): result = df.asfreq(freq=freq_depr) tm.assert_frame_equal(result, expected) + + def test_asfreq_unsupported_freq(self): + index = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M") + df = DataFrame({"a": Series([0, 1], index=index)}) + + try: + df.asfreq(freq="2MS") + except TypeError as err: + assert '"2MS" is not supported as a period frequency' == str(err) From 3c80b2dc098f4666efac55489da1bbc89f444ce7 Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Sun, 7 Jan 2024 18:54:18 +0100 Subject: [PATCH 3/8] pytest.raises --- pandas/tests/frame/methods/test_asfreq.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index 63dc2eba6e470..2c43a71d48d49 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -263,7 +263,7 @@ def test_asfreq_unsupported_freq(self): index = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M") df = DataFrame({"a": Series([0, 1], index=index)}) - try: + with pytest.raises( + TypeError, match='"2MS" is not supported as a period frequency' + ): df.asfreq(freq="2MS") - except TypeError as err: - assert '"2MS" is not supported as a period frequency' == str(err) From a1174ac890210e850cb1a26b26fdd64110a4408c Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Sun, 7 Jan 2024 22:42:15 +0100 Subject: [PATCH 4/8] freqstr --- pandas/core/arrays/period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 22f227abcfa68..b310ac8239c65 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -732,7 +732,7 @@ def asfreq(self, freq=None, how: str = "E") -> Self: PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01', '2015-01'], dtype='period[M]') """ - freq_str = freq if isinstance(freq, str) else "" + freq_str = freq if isinstance(freq, str) else freq.freqstr how = libperiod.validate_end_alias(how) if isinstance(freq, BaseOffset): From ad6f0e3e4b258a55f0ec369fdeae7597fd4eb253 Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Mon, 8 Jan 2024 01:22:42 +0100 Subject: [PATCH 5/8] add test for offsets --- pandas/tests/frame/methods/test_asfreq.py | 34 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index 2c43a71d48d49..a79484676eea8 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -259,11 +259,35 @@ def test_asfreq_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr): result = df.asfreq(freq=freq_depr) tm.assert_frame_equal(result, expected) - def test_asfreq_unsupported_freq(self): + @pytest.mark.parametrize( + "freq, error, error_msg", + [ + ( + "2MS", + TypeError, + '"2MS" is not supported as a period frequency', + ), + ( + offsets.MonthBegin(), + ValueError, + ( + "Invalid offset: '' for converting " + "time series with PeriodIndex." + ), + ), + ( + offsets.DateOffset(months=2), + ValueError, + ( + "Invalid offset: '' for converting " + "time series with PeriodIndex." + ), + ), + ], + ) + def test_asfreq_unsupported_freq(self, freq, error, error_msg): index = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M") df = DataFrame({"a": Series([0, 1], index=index)}) - with pytest.raises( - TypeError, match='"2MS" is not supported as a period frequency' - ): - df.asfreq(freq="2MS") + with pytest.raises(error, match=error_msg): + df.asfreq(freq=freq) From a606d55e63c8d3d4861e6b169a4cb077a6861df7 Mon Sep 17 00:00:00 2001 From: aram-cinnamon Date: Tue, 9 Jan 2024 18:26:59 +0100 Subject: [PATCH 6/8] rearrange per comment --- pandas/core/arrays/period.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index b310ac8239c65..dd115292168cd 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -732,15 +732,14 @@ def asfreq(self, freq=None, how: str = "E") -> Self: PeriodIndex(['2010-01', '2011-01', '2012-01', '2013-01', '2014-01', '2015-01'], dtype='period[M]') """ - freq_str = freq if isinstance(freq, str) else freq.freqstr - how = libperiod.validate_end_alias(how) + + freq_original = freq if isinstance(freq, BaseOffset): freq = freq_to_period_freqstr(freq.n, freq.name) freq = Period._maybe_convert_freq(freq) - if not hasattr(freq, "_period_dtype_code"): - raise TypeError(f'"{freq_str}" is not supported as a period frequency') + raise TypeError(f'"{freq_original}" is not supported as a period frequency') base1 = self._dtype._dtype_code base2 = freq._period_dtype_code From 721d15248476d64a1f8a1d4e35a109cccb2244ed Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:52:42 +0000 Subject: [PATCH 7/8] fixup --- pandas/tests/frame/methods/test_asfreq.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index a79484676eea8..ba9ea13ae48ee 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -286,6 +286,7 @@ def test_asfreq_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr): ], ) def test_asfreq_unsupported_freq(self, freq, error, error_msg): + # https://github.com/pandas-dev/pandas/issues/56718 index = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M") df = DataFrame({"a": Series([0, 1], index=index)}) From 52982ec08a679d64d23e9bd5fa249144b80d1e24 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 12 Feb 2024 17:37:11 +0000 Subject: [PATCH 8/8] fixup --- pandas/tests/frame/methods/test_asfreq.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index ba9ea13ae48ee..f6b71626b6fee 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -260,35 +260,26 @@ def test_asfreq_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr): tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( - "freq, error, error_msg", + "freq, error_msg", [ ( "2MS", - TypeError, - '"2MS" is not supported as a period frequency', + "MS is not supported as period frequency", ), ( offsets.MonthBegin(), - ValueError, - ( - "Invalid offset: '' for converting " - "time series with PeriodIndex." - ), + r"\ is not supported as period frequency", ), ( offsets.DateOffset(months=2), - ValueError, - ( - "Invalid offset: '' for converting " - "time series with PeriodIndex." - ), + r"\ is not supported as period frequency", ), ], ) - def test_asfreq_unsupported_freq(self, freq, error, error_msg): + def test_asfreq_unsupported_freq(self, freq, error_msg): # https://github.com/pandas-dev/pandas/issues/56718 index = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M") df = DataFrame({"a": Series([0, 1], index=index)}) - with pytest.raises(error, match=error_msg): + with pytest.raises(ValueError, match=error_msg): df.asfreq(freq=freq)