pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
29.98k stars 1.94k forks source link

adding `periods` argument to the date_range #7525

Open alonket opened 1 year ago

alonket commented 1 year ago

Problem description

I'm dealing once in a while with dates and I need to create date ranges. In pandas I can do it very easily: pd.date_range(pd.to_datetime(date) - pd.Timedelta(days=3), periods=3, freq="D").to_list()

However when I tried to convert it to polars I had to deal with end_date.

Following my question in SO, the answer pointed out(and I agree) that it could be a good Idea to have something like this: date_range(start_date, period_of_time, interval) in addition to date_range(start_date, end_date, interval)

MarcoGorelli commented 1 year ago

agree, being able to write pl.date_range(date(2020, 1, 1), periods=3) would be very convenient, I've found myself reaching for it a few times

andreas-vester commented 2 months ago

I totally agree. Introducing a periods argument is really helpful. See also my own question: https://stackoverflow.com/questions/78889556/create-date-range-with-predefined-number-of-periods-in-polars

This issue is open for quite some time now. I am wondering if there exist a concrete plan to incorporate it into the code base?

MarcoGorelli commented 2 months ago

@andreas-vester it's not high-priority, but if anyone from the community wanted to make a pull request, it would be welcome

MarcoGorelli commented 2 months ago

Made a start on this today:

In [1]: import polars as pl

In [2]: from datetime import date

In [3]: pl.date_range(date(2020,1,1), periods=3, eager=True)
Out[3]: 
shape: (3,)
Series: 'literal' [date]
[
        2020-01-01
        2020-01-02
        2020-01-03
]

In [4]: pl.date_range(date(2020,1,1), date(2020, 1, 5), eager=True)
Out[4]: 
shape: (5,)
Series: 'literal' [date]
[
        2020-01-01
        2020-01-02
        2020-01-03
        2020-01-04
        2020-01-05
]

No promises as to when it'll be finished