Skip to content

Commit 3191ab4

Browse files
committed
CLN: enforced the deprecation of strings ‘H’, ‘BH’, ‘CBH’ in favour of ‘h’, ‘bh’, ‘cbh’
1 parent a89f208 commit 3191ab4

File tree

6 files changed

+34
-71
lines changed

6 files changed

+34
-71
lines changed

pandas/_libs/tslibs/dtypes.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cdef bint is_supported_unit(NPY_DATETIMEUNIT reso)
1414
cdef dict c_OFFSET_TO_PERIOD_FREQSTR
1515
cdef dict c_PERIOD_TO_OFFSET_FREQSTR
1616
cdef dict c_OFFSET_RENAMED_FREQSTR
17-
cdef dict c_DEPR_ABBREVS
17+
cdef dict c_REMOVED_ABBREVS
1818
cdef dict c_DEPR_UNITS
1919
cdef dict c_PERIOD_AND_OFFSET_DEPR_FREQSTR
2020
cdef dict attrname_to_abbrevs

pandas/_libs/tslibs/dtypes.pyx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# period frequency constants corresponding to scikits timeseries
22
# originals
33
from enum import Enum
4-
import warnings
5-
6-
from pandas.util._exceptions import find_stack_level
74

85
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
96
from pandas._libs.tslibs.np_datetime cimport (
@@ -339,7 +336,7 @@ cdef dict c_OFFSET_TO_PERIOD_FREQSTR = OFFSET_TO_PERIOD_FREQSTR
339336
cdef dict c_PERIOD_TO_OFFSET_FREQSTR = PERIOD_TO_OFFSET_FREQSTR
340337

341338
# Map deprecated resolution abbreviations to correct resolution abbreviations
342-
cdef dict c_DEPR_ABBREVS = {
339+
cdef dict c_REMOVED_ABBREVS = {
343340
"H": "h",
344341
"BH": "bh",
345342
"CBH": "cbh",
@@ -362,6 +359,8 @@ cdef dict c_PERIOD_AND_OFFSET_DEPR_FREQSTR = {
362359
"MIN": "min",
363360
}
364361

362+
INVALID_FREQ_ERR_MSG = "Invalid frequency: {0}"
363+
365364

366365
class FreqGroup(Enum):
367366
# Mirrors c_FreqGroup in the .pxd file
@@ -451,19 +450,10 @@ class Resolution(Enum):
451450
>>> Resolution.get_reso_from_freqstr('h') == Resolution.RESO_HR
452451
True
453452
"""
454-
cdef:
455-
str abbrev
456453
try:
457-
if freq in c_DEPR_ABBREVS:
458-
abbrev = c_DEPR_ABBREVS[freq]
459-
warnings.warn(
460-
f"\'{freq}\' is deprecated and will be removed in a future "
461-
f"version. Please use \'{abbrev}\' "
462-
f"instead of \'{freq}\'.",
463-
FutureWarning,
464-
stacklevel=find_stack_level(),
465-
)
466-
freq = abbrev
454+
if freq in c_REMOVED_ABBREVS:
455+
456+
raise ValueError(INVALID_FREQ_ERR_MSG.format(freq))
467457
attr_name = _abbrev_to_attrnames[freq]
468458
except KeyError:
469459
# For quarterly and yearly resolutions, we need to chop off
@@ -474,16 +464,8 @@ class Resolution(Enum):
474464
if split_freq[1] not in _month_names:
475465
# i.e. we want e.g. "Q-DEC", not "Q-INVALID"
476466
raise
477-
if split_freq[0] in c_DEPR_ABBREVS:
478-
abbrev = c_DEPR_ABBREVS[split_freq[0]]
479-
warnings.warn(
480-
f"\'{split_freq[0]}\' is deprecated and will be removed in a "
481-
f"future version. Please use \'{abbrev}\' "
482-
f"instead of \'{split_freq[0]}\'.",
483-
FutureWarning,
484-
stacklevel=find_stack_level(),
485-
)
486-
split_freq[0] = abbrev
467+
if split_freq[0] in c_REMOVED_ABBREVS:
468+
raise ValueError(INVALID_FREQ_ERR_MSG.format(split_freq[0]))
487469
attr_name = _abbrev_to_attrnames[split_freq[0]]
488470

489471
return cls.from_attrname(attr_name)

pandas/_libs/tslibs/offsets.pyx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ from pandas._libs.tslibs.ccalendar cimport (
5656
)
5757
from pandas._libs.tslibs.conversion cimport localize_pydatetime
5858
from pandas._libs.tslibs.dtypes cimport (
59-
c_DEPR_ABBREVS,
6059
c_OFFSET_RENAMED_FREQSTR,
6160
c_OFFSET_TO_PERIOD_FREQSTR,
6261
c_PERIOD_AND_OFFSET_DEPR_FREQSTR,
6362
c_PERIOD_TO_OFFSET_FREQSTR,
63+
c_REMOVED_ABBREVS,
6464
periods_per_day,
6565
)
6666
from pandas._libs.tslibs.nattype cimport (
@@ -4908,15 +4908,8 @@ cpdef to_offset(freq, bint is_period=False):
49084908
if not stride:
49094909
stride = 1
49104910

4911-
if prefix in c_DEPR_ABBREVS:
4912-
warnings.warn(
4913-
f"\'{prefix}\' is deprecated and will be removed "
4914-
f"in a future version, please use "
4915-
f"\'{c_DEPR_ABBREVS.get(prefix)}\' instead.",
4916-
FutureWarning,
4917-
stacklevel=find_stack_level(),
4918-
)
4919-
prefix = c_DEPR_ABBREVS[prefix]
4911+
if prefix in c_REMOVED_ABBREVS:
4912+
raise ValueError(INVALID_FREQ_ERR_MSG.format(prefix))
49204913

49214914
if prefix in {"D", "h", "min", "s", "ms", "us", "ns"}:
49224915
# For these prefixes, we have something like "3h" or

pandas/tests/arrays/test_datetimes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ def test_date_range_frequency_M_Q_Y_raises(self, freq):
760760
with pytest.raises(ValueError, match=msg):
761761
pd.date_range("1/1/2000", periods=4, freq=freq)
762762

763-
@pytest.mark.parametrize("freq_depr", ["2H", "2CBH", "2MIN", "2S", "2mS", "2Us"])
763+
@pytest.mark.parametrize("freq_depr", ["2MIN", "2mS", "2Us"])
764764
def test_date_range_uppercase_frequency_deprecated(self, freq_depr):
765765
# GH#9586, GH#54939
766766
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed in a "
@@ -807,6 +807,13 @@ def test_date_range_frequency_A_raises(self, freq):
807807
with pytest.raises(ValueError, match=msg):
808808
pd.date_range("1/1/2000", periods=4, freq=freq)
809809

810+
@pytest.mark.parametrize("freq", ["2H", "2CBH", "2S"])
811+
def test_date_range_uppercase_frequency_raises(self, freq):
812+
msg = f"Invalid frequency: {freq}"
813+
814+
with pytest.raises(ValueError, match=msg):
815+
pd.date_range("1/1/2000", periods=4, freq=freq)
816+
810817

811818
def test_factorize_sort_without_freq():
812819
dta = DatetimeArray._from_sequence([0, 2, 1], dtype="M8[ns]")

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,12 @@ def test_asarray_tz_aware(self):
133133

134134
tm.assert_numpy_array_equal(result, expected)
135135

136-
def test_CBH_deprecated(self):
137-
msg = "'CBH' is deprecated and will be removed in a future version."
138-
139-
with tm.assert_produces_warning(FutureWarning, match=msg):
140-
expected = date_range(
141-
dt.datetime(2022, 12, 11), dt.datetime(2022, 12, 13), freq="CBH"
142-
)
143-
result = DatetimeIndex(
144-
[
145-
"2022-12-12 09:00:00",
146-
"2022-12-12 10:00:00",
147-
"2022-12-12 11:00:00",
148-
"2022-12-12 12:00:00",
149-
"2022-12-12 13:00:00",
150-
"2022-12-12 14:00:00",
151-
"2022-12-12 15:00:00",
152-
"2022-12-12 16:00:00",
153-
],
154-
dtype="datetime64[ns]",
155-
freq="cbh",
156-
)
136+
@pytest.mark.parametrize("freq", ["2H", "2BH", "2S"])
137+
def test_CBH_deprecated(self, freq):
138+
msg = f"Invalid frequency: {freq}"
157139

158-
tm.assert_index_equal(result, expected)
140+
with pytest.raises(ValueError, match=msg):
141+
date_range(dt.datetime(2022, 12, 11), dt.datetime(2022, 12, 13), freq=freq)
159142

160143
@pytest.mark.parametrize("freq", ["2BM", "1bm", "2BQ", "1BQ-MAR", "2BY-JUN", "1by"])
161144
def test_BM_BQ_BY_raises(self, freq):

pandas/tests/tslibs/test_to_offset.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,21 +206,19 @@ def test_to_offset_lowercase_frequency_raises(freq_depr):
206206
to_offset(freq_depr)
207207

208208

209-
@pytest.mark.parametrize(
210-
"freq_depr",
211-
[
212-
"2H",
213-
"2BH",
214-
"2MIN",
215-
"2S",
216-
"2Us",
217-
"2NS",
218-
],
219-
)
209+
@pytest.mark.parametrize("freq_depr", ["2MIN", "2Us", "2NS"])
220210
def test_to_offset_uppercase_frequency_deprecated(freq_depr):
221211
# GH#54939
222212
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed in a "
223213
f"future version, please use '{freq_depr.lower()[1:]}' instead."
224214

225215
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
226216
to_offset(freq_depr)
217+
218+
219+
@pytest.mark.parametrize("freq", ["2H", "2BH", "2S"])
220+
def test_to_offset_uppercase_frequency_raises(freq):
221+
msg = f"Invalid frequency: {freq}"
222+
223+
with pytest.raises(ValueError, match=msg):
224+
to_offset(freq)

0 commit comments

Comments
 (0)