Skip to content

Commit

Permalink
ENH: unify warning message in to_offset() (#56417)
Browse files Browse the repository at this point in the history
* ENH: unify warning message for deprecated freqs

* correct warning message for deprecated abbreviations
  • Loading branch information
natmokval authored Dec 12, 2023
1 parent acc395a commit f571cce
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4786,7 +4786,8 @@ cpdef to_offset(freq, bint is_period=False):
for n, (sep, stride, name) in enumerate(tups):
if is_period is False and name in c_OFFSET_DEPR_FREQSTR:
warnings.warn(
f"\'{name}\' is deprecated, please use "
f"\'{name}\' is deprecated and will be removed "
f"in a future version, please use "
f"\'{c_OFFSET_DEPR_FREQSTR.get(name)}\' instead.",
FutureWarning,
stacklevel=find_stack_level(),
Expand Down Expand Up @@ -4829,9 +4830,8 @@ cpdef to_offset(freq, bint is_period=False):
if prefix in c_DEPR_ABBREVS:
warnings.warn(
f"\'{prefix}\' is deprecated and will be removed "
f"in a future version. Please use "
f"\'{c_DEPR_ABBREVS.get(prefix)}\' "
f"instead of \'{prefix}\'.",
f"in a future version, please use "
f"\'{c_DEPR_ABBREVS.get(prefix)}\' instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ def test_iter_zoneinfo_fold(self, tz):
)
def test_date_range_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
# GH#9586, GH#54275
depr_msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

expected = pd.date_range("1/1/2000", periods=4, freq=freq)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ def test_asfreq_2ME(self, freq, freq_half):
)
def test_asfreq_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
# GH#9586, #55978
depr_msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

index = date_range("1/1/2000", periods=4, freq=f"{freq[1:]}")
df = DataFrame({"s": Series([0.0, 1.0, 2.0, 3.0], index=index)})
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/datetimes/methods/test_to_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def test_dti_to_period_2monthish(self, freq_offset, freq_period):
)
def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
# GH#9586
msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
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()
Expand All @@ -119,7 +120,8 @@ def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
)
def test_to_period_frequency_BQ_BY_deprecated(self, freq, freq_depr):
# GH#9586
msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
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()
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def test_date_range_fractional_period(self):
)
def test_date_range_frequency_M_SM_BQ_BY_deprecated(self, freq, freq_depr):
# GH#52064
depr_msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

expected = date_range("1/1/2000", periods=4, freq=freq)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
Expand Down Expand Up @@ -788,8 +789,12 @@ def test_freq_dateoffset_with_relateivedelta_nanos(self):
)
def test_frequencies_H_T_S_L_U_N_deprecated(self, freq, freq_depr):
# GH#52536
freq_msg = re.split("[0-9]*", freq_depr, maxsplit=1)[1]
msg = f"'{freq_msg}' is deprecated and will be removed in a future version."
freq_msg = re.split("[0-9]*", freq, maxsplit=1)[1]
freq_depr_msg = re.split("[0-9]*", freq_depr, maxsplit=1)[1]
msg = (
f"'{freq_depr_msg}' is deprecated and will be removed in a future version, "
)
f"please use '{freq_msg}' instead"

expected = date_range("1/1/2000", periods=2, freq=freq)
with tm.assert_produces_warning(FutureWarning, match=msg):
Expand All @@ -809,7 +814,8 @@ def test_frequencies_A_deprecated_Y_renamed(self, freq, freq_depr):
# GH#9586, GH#54275
freq_msg = re.split("[0-9]*", freq, maxsplit=1)[1]
freq_depr_msg = re.split("[0-9]*", freq_depr, maxsplit=1)[1]
msg = f"'{freq_depr_msg}' is deprecated, please use '{freq_msg}' instead."
msg = f"'{freq_depr_msg}' is deprecated and will be removed "
f"in a future version, please use '{freq_msg}' instead."

expected = date_range("1/1/2000", periods=2, freq=freq)
with tm.assert_produces_warning(FutureWarning, match=msg):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ def test_AS_BAS_deprecated(self, freq_depr, expected_values, expected_freq):
)
def test_BM_BQ_BY_deprecated(self, freq, expected_values, freq_depr):
# GH#52064
msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

with tm.assert_produces_warning(FutureWarning, match=msg):
expected = date_range(start="2016-02-21", end="2016-08-21", freq=freq_depr)
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,8 @@ def test_resample_empty_series_with_tz():
)
def test_resample_M_Q_Y_A_deprecated(freq, freq_depr):
# GH#9586
depr_msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

s = Series(range(10), index=date_range("20130101", freq="d", periods=10))
expected = s.resample(freq).mean()
Expand All @@ -2121,7 +2122,8 @@ def test_resample_M_Q_Y_A_deprecated(freq, freq_depr):
)
def test_resample_BM_BQ_deprecated(freq, freq_depr):
# GH#52064
depr_msg = f"'{freq_depr[1:]}' is deprecated, please use '{freq[1:]}' instead."
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
f"in a future version, please use '{freq[1:]}' instead."

s = Series(range(10), index=date_range("20130101", freq="d", periods=10))
expected = s.resample(freq).mean()
Expand Down

0 comments on commit f571cce

Please sign in to comment.