polars.date_range#

polars.date_range(low: date | datetime, high: date | datetime, interval: str | timedelta, closed: ClosedWindow = 'both', name: str | None = None, time_unit: TimeUnit | None = None) pli.Series[source]#

Create a range of type Datetime (or Date).

Parameters:
low

Lower bound of the date range.

high

Upper bound of the date range.

interval

Interval periods. It can be a python timedelta object, like timedelta(days=10), or a polars duration string, such as 3d12h4m25s representing 3 days, 12 hours, 4 minutes, and 25 seconds.

closed{‘both’, ‘left’, ‘right’, ‘none’}

Define whether the temporal window interval is closed or not.

name

Name of the output Series.

time_unit{None, ‘ns’, ‘us’, ‘ms’}

Set the time unit.

Returns:
A Series of type Datetime or Date.

Notes

If both low and high are passed as date types (not datetime), and the interval granularity is no finer than 1d, the returned range is also of type date. All other permutations return a datetime Series.

Examples

Using polars duration string to specify the interval:

>>> from datetime import date
>>> pl.date_range(date(2022, 1, 1), date(2022, 3, 1), "1mo", name="drange")
shape: (3,)
Series: 'drange' [date]
[
    2022-01-01
    2022-02-01
    2022-03-01
]

Using timedelta object to specify the interval:

>>> from datetime import datetime, timedelta
>>> pl.date_range(
...     datetime(1985, 1, 1),
...     datetime(1985, 1, 10),
...     timedelta(days=1, hours=12),
...     time_unit="ms",
... )
shape: (7,)
Series: '' [datetime[ms]]
[
    1985-01-01 00:00:00
    1985-01-02 12:00:00
    1985-01-04 00:00:00
    1985-01-05 12:00:00
    1985-01-07 00:00:00
    1985-01-08 12:00:00
    1985-01-10 00:00:00
]