Skip to content

Commit

Permalink
Fix pandas deprecation warnings
Browse files Browse the repository at this point in the history
Fixes pandas 2.2 deprecation warnings concerning deprecation of
frequency units "T", "H" and "S" in favor of "min", "h" and "s".
  • Loading branch information
maread99 committed Jan 31, 2024
1 parent 4b8fd5c commit 63087a0
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 137 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Timestamp('2022-01-03 00:00:00', freq='C')
Timestamp('2022-01-10 00:00:00', freq='C')

>>> xhkg.trading_index(
... "2021-12-30", "2021-12-31", period="90T", force=True
... "2021-12-30", "2021-12-31", period="90min", force=True
... )
IntervalIndex([[2021-12-30 01:30:00, 2021-12-30 03:00:00), [2021-12-30 03:00:00, 2021-12-30 04:00:00), [2021-12-30 05:00:00, 2021-12-30 06:30:00), [2021-12-30 06:30:00, 2021-12-30 08:00:00), [2021-12-31 01:30:00, 2021-12-31 03:00:00), [2021-12-31 03:00:00, 2021-12-31 04:00:00)], dtype='interval[datetime64[ns, UTC], left]')
```
Expand Down
8 changes: 4 additions & 4 deletions exchange_calendars/calendar_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ def parse_timestamp(
)
side = side if side is not None else calendar.side
if side == "left":
ts = ts.floor("T")
ts = ts.floor("min")
elif side == "right":
ts = ts.ceil("T")
ts = ts.ceil("min")
else:
raise ValueError(
"`timestamp` cannot have a non-zero second (or more accurate)"
Expand Down Expand Up @@ -493,7 +493,7 @@ def align_opens(opens: pd.Series, align: pd.Timedelta) -> np.ndarray:
opens = opens.dt.ceil(align)
return opens.values.astype(np.int64)

if align != pd.Timedelta(1, "T"):
if align != pd.Timedelta(1, "min"):
self.opens = align_opens(calendar.opens[slce], align)
else:
self.opens = calendar.opens_nanos[slce]
Expand All @@ -503,7 +503,7 @@ def align_opens(opens: pd.Series, align: pd.Timedelta) -> np.ndarray:
else:
self.break_starts = calendar.break_starts_nanos[slce]

if align_pm != pd.Timedelta(1, "T"):
if align_pm != pd.Timedelta(1, "min"):
self.break_ends = align_opens(calendar.break_ends[slce], align_pm)
else:
self.break_ends = calendar.break_ends_nanos[slce]
Expand Down
18 changes: 9 additions & 9 deletions exchange_calendars/exchange_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,7 @@ def trading_index(
force: bool | None = None,
curtail_overlaps: bool = False,
ignore_breaks: bool = False,
align: pd.Timedelta | str = pd.Timedelta(1, "T"),
align: pd.Timedelta | str = pd.Timedelta(1, "min"),
align_pm: pd.Timedelta | bool = True,
parse: bool = True,
) -> pd.DatetimeIndex | pd.IntervalIndex:
Expand Down Expand Up @@ -2538,7 +2538,7 @@ def trading_index(
component then can pass `parse` as False to save around
500µs on the execution.
align : default: pd.Timedelta(1, "T")
align : default: pd.Timedelta(1, "min")
Anchor the first indice of each session such that it aligns
with the nearest occurrence of a specific fraction of an hour.
Expand All @@ -2547,14 +2547,14 @@ def trading_index(
forwards, -ve values to shift indices backwards.
Valid values are (or equivalent):
"2T", "3T", "4T", "5T", "6T", "10T", "12T", "15T", "20T",
"30T", "-2T", "-4T", "-5T", "-6T", "-10T", "-12T", "-15T",
"-20T", "-30T"
"2min", "3min", "4min", "5min", "6min", "10min", "12min", "15min", "20min",
"30min", "-2min", "-4min", "-5min", "-6min", "-10min", "-12min", "-15min",
"-20min", "-30min"
For example, if `intervals` is True and `period` is '5T' then
the first interval of a session with open time as 07:59 would
be:
07:59 - 08:04 if `align` is pd.Timedelta(1, "T") (default)
07:59 - 08:04 if `align` is pd.Timedelta(1, "min") (default)
08:00 - 08:05 if `align` is '5T'
07:55 - 08:00 if `align` is '-5T'
Expand Down Expand Up @@ -2662,13 +2662,13 @@ def get_align(name: Literal["align", "align_pm"], value: Any) -> pd.Timedelta:
)
raise ValueError(msg) from None

ONE_HOUR = pd.Timedelta("1H")
ONE_HOUR = pd.Timedelta("1h")
if value > ONE_HOUR or value < -ONE_HOUR or not value or (ONE_HOUR % value):
raise ValueError(
f"`{name}` must be factor of 1H although received '{value}'."
)

if value % pd.Timedelta(1, "T"):
if value % pd.Timedelta(1, "min"):
raise ValueError(
f"`{name}` cannot include a fraction of a minute although received"
f" '{value}'."
Expand All @@ -2678,7 +2678,7 @@ def get_align(name: Literal["align", "align_pm"], value: Any) -> pd.Timedelta:
align = get_align("align", align)

if align_pm is False:
align_pm = pd.Timedelta(1, "T")
align_pm = pd.Timedelta(1, "min")
else:
align_pm = align if align_pm is True else get_align("align_pm", align_pm)

Expand Down
4 changes: 2 additions & 2 deletions exchange_calendars/utils/pandas_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def indexes_union(indexes: list[pd.Index]) -> pd.Index:
Examples
--------
>>> index1 = pd.date_range('2021-05-01 12:20', periods=2, freq='1H')
>>> index2 = pd.date_range('2021-05-02 17:10', periods=2, freq='22T')
>>> index1 = pd.date_range('2021-05-01 12:20', periods=2, freq='1h')
>>> index2 = pd.date_range('2021-05-02 17:10', periods=2, freq='22min')
>>> index3 = pd.date_range('2021-05-03', periods=2, freq='1D')
>>> indexes_union([index1, index2, index3])
DatetimeIndex(['2021-05-01 12:20:00', '2021-05-01 13:20:00',
Expand Down
Loading

0 comments on commit 63087a0

Please sign in to comment.