Skip to content

Commit

Permalink
Add "closed" arg to date_range
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lacroix authored and ritchie46 committed Oct 31, 2021
1 parent 09e27f5 commit bc9e7da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 0 additions & 3 deletions py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1924,9 +1924,6 @@ def upsample(
low: datetime = bounds[0, "low"]
high: datetime = bounds[0, "high"]
upsampled = pl.date_range(low, high, interval, name=by)
# pl.date_range excludes 'high'. Include it when relevant
if (high - low) % interval == timedelta(0):
upsampled.append(pl.Series(values=[high]))
return pl.DataFrame(upsampled).join(self, on=by, how="left")

def join(
Expand Down
18 changes: 13 additions & 5 deletions py-polars/polars/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ def arg_where(mask: "pl.Series") -> "pl.Series":


def date_range(
low: datetime, high: datetime, interval: timedelta, name: Optional[str] = None
low: datetime,
high: datetime,
interval: timedelta,
closed: Optional[str] = None,
name: Optional[str] = None,
) -> pl.Series:
"""
Create a date range of type `Datetime`.
Expand All @@ -109,6 +113,8 @@ def date_range(
Upper bound of the date range
interval
Interval periods
closed {None, 'left', 'right'}
Make the interval closed to the 'left', 'right', or both sides (None, the default).
name
Name of the output Series
Returns
Expand Down Expand Up @@ -150,7 +156,9 @@ def date_range(
2015-06-30 12:00:00
]
"""
return pl.Series(
name=name,
values=np.arange(low, high, interval, dtype="datetime64[ms]").astype(int),
).cast(pl.Datetime)
values = np.arange(low, high, interval, dtype="datetime64[ms]")
if closed in (None, "right") and (high - low) % interval == timedelta(0):
values = np.append(values, np.array(high, dtype="datetime64[ms]"))
if closed == "right":
values = values[1:]
return pl.Series(name=name, values=values.astype(int)).cast(pl.Datetime)

0 comments on commit bc9e7da

Please sign in to comment.