Skip to content

Commit 55c9fbd

Browse files
Backport PR #56906 on branch 2.2.x (DEPR: freq ''2BQ-SEP" for to_period should raise an error) (#56916)
Backport PR #56906: DEPR: freq ''2BQ-SEP" for to_period should raise an error Co-authored-by: Natalia Mokeeva <[email protected]>
1 parent 8e25417 commit 55c9fbd

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

pandas/core/arrays/datetimes.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
tz_convert_from_utc,
4040
tzconversion,
4141
)
42-
from pandas._libs.tslibs.dtypes import (
43-
abbrev_to_npy_unit,
44-
freq_to_period_freqstr,
45-
)
42+
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
4643
from pandas.errors import PerformanceWarning
4744
from pandas.util._exceptions import find_stack_level
4845
from pandas.util._validators import validate_inclusive
@@ -1232,8 +1229,10 @@ def to_period(self, freq=None) -> PeriodArray:
12321229

12331230
if freq is None:
12341231
freq = self.freqstr or self.inferred_freq
1235-
if isinstance(self.freq, BaseOffset):
1236-
freq = freq_to_period_freqstr(self.freq.n, self.freq.name)
1232+
if isinstance(self.freq, BaseOffset) and hasattr(
1233+
self.freq, "_period_dtype_code"
1234+
):
1235+
freq = PeriodDtype(self.freq)._freqstr
12371236

12381237
if freq is None:
12391238
raise ValueError(

pandas/tests/indexes/datetimes/methods/test_to_period.py

-17
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,6 @@ def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
111111
with tm.assert_produces_warning(FutureWarning, match=msg):
112112
assert prng.freq == freq_depr
113113

114-
@pytest.mark.parametrize(
115-
"freq, freq_depr",
116-
[
117-
("2BQE-SEP", "2BQ-SEP"),
118-
("2BYE-MAR", "2BY-MAR"),
119-
],
120-
)
121-
def test_to_period_frequency_BQ_BY_deprecated(self, freq, freq_depr):
122-
# GH#9586
123-
msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
124-
f"in a future version, please use '{freq[1:]}' instead."
125-
126-
rng = date_range("01-Jan-2012", periods=8, freq=freq)
127-
prng = rng.to_period()
128-
with tm.assert_produces_warning(FutureWarning, match=msg):
129-
prng.freq == freq_depr
130-
131114
def test_to_period_infer(self):
132115
# https://github.com/pandas-dev/pandas/issues/33358
133116
rng = date_range(

pandas/tests/indexes/period/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def test_period_index_frequency_invalid_freq(self, freq_depr):
4848
with pytest.raises(ValueError, match=msg):
4949
PeriodIndex(["2020-01", "2020-05"], freq=freq_depr)
5050

51+
@pytest.mark.parametrize("freq", ["2BQE-SEP", "2BYE-MAR", "2BME"])
52+
def test_period_index_from_datetime_index_invalid_freq(self, freq):
53+
# GH#56899
54+
msg = f"Invalid frequency: {freq[1:]}"
55+
56+
rng = date_range("01-Jan-2012", periods=8, freq=freq)
57+
with pytest.raises(ValueError, match=msg):
58+
rng.to_period()
59+
5160

5261
class TestPeriodIndex:
5362
def test_from_ordinals(self):

0 commit comments

Comments
 (0)