Skip to content

DEPR: correct error message in to_offset #56141

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 2 commits into from
Nov 24, 2023
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
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,9 @@ cpdef to_offset(freq, bint is_period=False):
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 "
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]}'"

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 19 additions & 3 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)