Skip to content

Commit

Permalink
Backport PR pandas-dev#56906: DEPR: freq ''2BQ-SEP" for to_period sho…
Browse files Browse the repository at this point in the history
…uld raise an error
  • Loading branch information
natmokval authored and meeseeksmachine committed Jan 16, 2024
1 parent 8e25417 commit 8504ac8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
11 changes: 5 additions & 6 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
tz_convert_from_utc,
tzconversion,
)
from pandas._libs.tslibs.dtypes import (
abbrev_to_npy_unit,
freq_to_period_freqstr,
)
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
from pandas.errors import PerformanceWarning
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_inclusive
Expand Down Expand Up @@ -1232,8 +1229,10 @@ def to_period(self, freq=None) -> PeriodArray:

if freq is None:
freq = self.freqstr or self.inferred_freq
if isinstance(self.freq, BaseOffset):
freq = freq_to_period_freqstr(self.freq.n, self.freq.name)
if isinstance(self.freq, BaseOffset) and hasattr(
self.freq, "_period_dtype_code"
):
freq = PeriodDtype(self.freq)._freqstr

if freq is None:
raise ValueError(
Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/indexes/datetimes/methods/test_to_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,6 @@ def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
with tm.assert_produces_warning(FutureWarning, match=msg):
assert prng.freq == freq_depr

@pytest.mark.parametrize(
"freq, freq_depr",
[
("2BQE-SEP", "2BQ-SEP"),
("2BYE-MAR", "2BY-MAR"),
],
)
def test_to_period_frequency_BQ_BY_deprecated(self, freq, freq_depr):
# GH#9586
msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

rng = date_range("01-Jan-2012", periods=8, freq=freq)
prng = rng.to_period()
with tm.assert_produces_warning(FutureWarning, match=msg):
prng.freq == freq_depr

def test_to_period_infer(self):
# https://github.com/pandas-dev/pandas/issues/33358
rng = date_range(
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/period/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def test_period_index_frequency_invalid_freq(self, freq_depr):
with pytest.raises(ValueError, match=msg):
PeriodIndex(["2020-01", "2020-05"], freq=freq_depr)

@pytest.mark.parametrize("freq", ["2BQE-SEP", "2BYE-MAR", "2BME"])
def test_period_index_from_datetime_index_invalid_freq(self, freq):
# GH#56899
msg = f"Invalid frequency: {freq[1:]}"

rng = date_range("01-Jan-2012", periods=8, freq=freq)
with pytest.raises(ValueError, match=msg):
rng.to_period()


class TestPeriodIndex:
def test_from_ordinals(self):
Expand Down

0 comments on commit 8504ac8

Please sign in to comment.