Skip to content

Commit 9296b6e

Browse files
committed
ERR: Improve error message for deprecated frequency aliases
1 parent 29662c0 commit 9296b6e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pandas/tests/tseries/frequencies/test_frequencies.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from pandas._libs.tslibs import offsets
44

5+
import pandas as pd
6+
57
from pandas.tseries.frequencies import (
68
is_subperiod,
79
is_superperiod,
@@ -27,3 +29,9 @@
2729
def test_super_sub_symmetry(p1, p2, expected):
2830
assert is_superperiod(p1, p2) is expected
2931
assert is_subperiod(p2, p1) is expected
32+
33+
34+
def test_deprecated_freq_alias_error():
35+
# GH#62259
36+
with pytest.raises(ValueError, match="Invalid frequency 'H'.*Did you mean 'h'?"):
37+
pd.date_range("2012-01-01", periods=3, freq="H")

pandas/tseries/frequencies.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pandas._libs.tslibs.offsets import (
2828
DateOffset,
2929
Day,
30-
to_offset,
30+
to_offset as _to_offset,
3131
)
3232
from pandas._libs.tslibs.parsing import get_rule_month
3333
from pandas.util._decorators import cache_readonly
@@ -53,6 +53,12 @@
5353
TimedeltaIndex,
5454
)
5555
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
56+
57+
_DEPRECATED_FREQ_ALIASES = {
58+
"H": "h",
59+
"T": "min",
60+
"S": "s",
61+
}
5662
# --------------------------------------------------------------------
5763
# Offset related functions
5864

@@ -600,6 +606,21 @@ def _is_weekly(rule: str) -> bool:
600606
return rule == "W" or rule.startswith("W-")
601607

602608

609+
def to_offset(freq):
610+
try:
611+
return _to_offset(freq)
612+
except Exception as err:
613+
if isinstance(freq, str) and freq in _DEPRECATED_FREQ_ALIASES:
614+
suggestion = _DEPRECATED_FREQ_ALIASES[freq]
615+
raise ValueError(
616+
f"Invalid frequency '{freq}'. This alias was deprecated and removed. "
617+
f"Did you mean '{suggestion}'?"
618+
) from None
619+
raise ValueError(
620+
f"Invalid frequency: {freq}, failed to parse with error message: {err}"
621+
) from None
622+
623+
603624
__all__ = [
604625
"Day",
605626
"get_period_alias",

0 commit comments

Comments
 (0)