pola-rs / polars

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

Add support for `time-rs` crate. #14147

Open avhz opened 6 months ago

avhz commented 6 months ago

Description

I would like to be able to create date columns from the time-rs crate (in addition to the currently supported chrono input).

Is this something that would be possible? I could also try to do it myself if no one is keen enough to do it.

BGR360 commented 6 months ago

Can you please show an example of the code you would like to be able to write?

avhz commented 6 months ago

For example, something like the example in the Polars docs here:

use chrono::prelude::*;

let date = polars::time::date_range(
    "date",
    NaiveDate::from_ymd_opt(2022, 1, 1)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap(),
    NaiveDate::from_ymd_opt(2022, 1, 5)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap(),
    Duration::parse("1d"),
    ClosedWindow::Both,
    TimeUnit::Milliseconds,
    None,
)?
.cast(&DataType::Date)?;

let datetime = polars::time::date_range(
    "datetime",
    NaiveDate::from_ymd_opt(2022, 1, 1)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap(),
    NaiveDate::from_ymd_opt(2022, 1, 5)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap(),
    Duration::parse("1d"),
    ClosedWindow::Both,
    TimeUnit::Milliseconds,
    None,
)?;

let df = df! (
    "date" => date,
    "datetime" => datetime,
)?;

But replace chrono's NaiveDate with time's PrimitiveDatetime, for example.

avhz commented 5 months ago

Any thoughts on this ?

avhz commented 3 months ago

To be more precise, I would like to do something like:

use time::macros::{date, datetime};

let df: DataFrame = df!(
    "date" => &[
        date!(2020-01-01), 
        date!(2021-01-01), 
        date!(2022-01-01)
    ],
    "datetime" => &[
        datetime!(2020-01-01 0:00), 
        datetime!(2021-01-01 0:00), 
        datetime!(2022-01-01 0:00)
    ],
)
.unwrap();

println!("{}", df);