Skip to content

Commit

Permalink
correct cpdef to_offset, add tests for warning
Browse files Browse the repository at this point in the history
  • Loading branch information
natmokval committed Sep 27, 2023
1 parent dbd1da3 commit 935280e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
22 changes: 12 additions & 10 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4641,15 +4641,6 @@ cpdef to_offset(freq, bint is_period=False):
if not stride:
stride = 1

if prefix in c_DEPR_ABBREVS:
warnings.warn(
f"\'{prefix}\' is deprecated and will be removed in a "
f"future version. Please use \'{c_DEPR_ABBREVS.get(prefix)}\' "
f"instead of \'{prefix}\'.",
FutureWarning,
stacklevel=find_stack_level(),
)
prefix = c_DEPR_ABBREVS[prefix]
if prefix in {"D", "H", "min", "s", "ms", "us", "ns"}:
# For these prefixes, we have something like "3H" or
# "2.5T", so we can construct a Timedelta with the
Expand All @@ -4662,8 +4653,19 @@ cpdef to_offset(freq, bint is_period=False):
# into the offset
offset *= stride_sign
else:
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}\'.",
FutureWarning,
stacklevel=find_stack_level(),
)
prefix = c_DEPR_ABBREVS[prefix]

stride = int(stride)
offset = _get_offset(name)
offset = _get_offset(prefix)
offset = offset * int(np.fabs(stride) * stride_sign)

if delta is None:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,14 +838,15 @@ def test_freq_dateoffset_with_relateivedelta_nanos(self):
@pytest.mark.parametrize(
"freq,freq_depr",
[
("Y", "A"),
("min", "T"),
("s", "S"),
("ms", "L"),
("us", "U"),
("ns", "N"),
],
)
def test_frequencies_T_S_L_U_N_deprecated(self, freq, freq_depr):
def test_frequencies_a_t_s_l_u_n_deprecated(self, freq, freq_depr):
# GH#52536
msg = f"'{freq_depr}' is deprecated and will be removed in a future version."

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ def test_period_index_frequency_ME_error_message(self):
with pytest.raises(ValueError, match=msg):
PeriodIndex(["2020-01-01", "2020-01-02"], freq="2ME")

def test_a_deprecated_from_time_series(self):
# GH#52536
msg = "'A' is deprecated and will be removed in a future version."

with tm.assert_produces_warning(FutureWarning, match=msg):
index = period_range(freq="A", start="1/1/2001", end="12/1/2009")
series = Series(1, index=index)
assert isinstance(series, Series)


def test_maybe_convert_timedelta():
pi = PeriodIndex(["2000", "2001"], freq="D")
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/tseries/frequencies/test_freq_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def test_compatibility(freqstr, expected):
assert ts_np + do == np.datetime64(expected)


@pytest.mark.parametrize("freq", ["T", "S", "L", "N", "U"])
def test_units_t_l_deprecated_from__attrname_to_abbrevs(freq):
# GH 52536
@pytest.mark.parametrize("freq", ["A", "T", "S", "L", "U", "N"])
def test_units_a_t_s_l_u_n_deprecated_from_attrname_to_abbrevs(freq):
# GH#52536
msg = f"'{freq}' is deprecated and will be removed in a future version."

with tm.assert_produces_warning(FutureWarning, match=msg):
Expand Down

0 comments on commit 935280e

Please sign in to comment.