Skip to content

DEPR: freq ''2BQ-SEP" for to_period should raise an error #56906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,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 @@ -1242,8 +1239,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