Skip to content
Open
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
8 changes: 8 additions & 0 deletions pandas/tests/tseries/frequencies/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pandas._libs.tslibs import offsets

import pandas as pd

from pandas.tseries.frequencies import (
is_subperiod,
is_superperiod,
Expand All @@ -27,3 +29,9 @@
def test_super_sub_symmetry(p1, p2, expected):
assert is_superperiod(p1, p2) is expected
assert is_subperiod(p2, p1) is expected


def test_deprecated_freq_alias_error():
# GH#62259
with pytest.raises(ValueError, match="Invalid frequency 'H'.*Did you mean 'h'?"):
pd.date_range("2012-01-01", periods=3, freq="H")
22 changes: 21 additions & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pandas._libs.tslibs.offsets import (
DateOffset,
Day,
to_offset,
to_offset as _to_offset,
)
from pandas._libs.tslibs.parsing import get_rule_month
from pandas.util._decorators import cache_readonly
Expand All @@ -53,6 +53,12 @@
TimedeltaIndex,
)
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin

_DEPRECATED_FREQ_ALIASES = {
"H": "h",
"T": "min",
"S": "s",
}
# --------------------------------------------------------------------
# Offset related functions

Expand Down Expand Up @@ -600,6 +606,20 @@ def _is_weekly(rule: str) -> bool:
return rule == "W" or rule.startswith("W-")


def to_offset(freq):
try:
return _to_offset(freq)
except Exception as err:
if isinstance(freq, str) and freq in _DEPRECATED_FREQ_ALIASES:
suggestion = _DEPRECATED_FREQ_ALIASES[freq]
raise ValueError(
f"Invalid frequency '{freq}'. Did you mean '{suggestion}'?"
) from None
raise ValueError(
f"Invalid frequency: {freq}, failed to parse with error message: {err}"
) from None


__all__ = [
"Day",
"get_period_alias",
Expand Down
Loading