Skip to content

Commit

Permalink
refactor(ranges): use appropriate Polars type aliases (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
baggiponte authored Feb 22, 2024
1 parent 2d63ad3 commit b920c27
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions functime/ranges.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

import polars as pl
from polars.type_aliases import TimeUnit

from functime.offsets import _strip_freq_alias

Expand All @@ -10,42 +11,36 @@ def make_future_ranges(
cutoffs: pl.DataFrame,
fh: int,
freq: str,
time_unit: Optional[str] = None,
time_unit: Optional[TimeUnit] = None,
) -> pl.DataFrame:
"""Return pl.DataFrame with columns entity_col, time_col.
DataFrame has shape (n_entities, 2) and dtypes (str, list[date]), (str, list[datetime]), or (str, list[int]).
"""
entity_col = cutoffs.columns[0]
if freq.endswith("i"):
future_ranges = cutoffs.select(
[
pl.col(entity_col),
pl.int_ranges(
pl.col("low") + 1,
pl.col("low") + fh + 1,
step=int(freq[:-1]),
eager=False,
).alias(time_col),
]
return cutoffs.select(
pl.col(entity_col),
pl.int_ranges(
pl.col("low") + 1,
pl.col("low") + fh + 1,
step=int(freq[:-1]),
eager=False,
).alias(time_col),
)
else:
time_unit = time_unit or "us"
offset_n, offset_alias = _strip_freq_alias(freq)
# Make date ranges
future_ranges = cutoffs.select(
[
pl.col(entity_col),
pl.date_ranges(
pl.col("low"),
pl.col("low")
.dt.offset_by(f"{fh * offset_n}{offset_alias}")
.alias("high"),
interval=freq,
closed="right",
time_unit=time_unit,
eager=False,
).alias(time_col),
]
return cutoffs.select(
pl.col(entity_col),
pl.date_ranges(
pl.col("low"),
pl.col("low")
.dt.offset_by(f"{fh * offset_n}{offset_alias}")
.alias("high"),
interval=freq,
closed="right",
time_unit=time_unit or "us",
eager=False,
).alias(time_col),
)
return future_ranges

0 comments on commit b920c27

Please sign in to comment.