Add a new Date column based off an existing Date column in rust polars

bashford7

I am trying to add a new column in my polars DataFrame: 'for_date'

There is already a column named 'made_on', and I want 'for_date' to be 1 day greater than 'made_on'. So if 'made_on' is 2022-12-25 then for_date should be 2022-12-26 etc.

My df looks like this:

┌────────────┐
│ made_on    │
│ ---        │
│ date       │
╞════════════╡
│ 2021-06-04 │
│ 2021-12-31 │
│ 2021-12-31 │
│ 2021-12-30 │
│ ...        │
│ 2022-08-06 │
│ 2022-08-06 │
│ 2022-08-06 │
│ 2022-08-06 │
└────────────┘

I know I can use with_column() on the df to transform a column but can't seem to get the types working and as a Rust newbie it's driving me a bit mad! In pseudo code I am looking for:

let df = df.with_column((col("made_on").map(/* add one day */).alias("for_date"))
Nathan Kleyn

If you ensure you have the dtype-duration and temporal features enabled for the polars crate (these features may be on by default in some setups or versions):

polars = { version = "0.27.2", features = ["dtype-duration", "lazy", "temporal"] }

Then you can simply add a Chrono Duration to the date/time column you have:

use polars::{prelude::*, export::chrono};

fn main() -> PolarsResult<()> {
    let df = CsvReader::from_path("example.csv")?
        .infer_schema(None)
        .has_header(true)
        .with_parse_dates(true)
        .finish()?
        .lazy()
        .with_column(
            (col("made_on") + chrono::Duration::days(1).lit()).alias("for_date")
        );

    println!("{:?}", df.collect());

    Ok(())
}

This is not very well documented through examples, but the corresponding RustDocs for the literal values of Chrono Duration shed some light.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python : Add a new column with date based on existing column in dataframe

XML read column and add new data based off date format

Cumulatively add month to a Date column based off the first date of a group

How can I groupby on the Year or Weekday of a date column in Polars Rust

Creating a new date column based on a condition and an existing date column and time delta column

Add date to multiple cells based off another column in Excel

SQL - add new column based off of existing column value (1 row)

Add column/series to dataframe in polars rust

Update the new date column using the existing date column with -1 day

Python calculate a new column based on Date column

Add new column based on a list and sort date by newest

add new column based on the several rows which have the same date

How to insert date into new column based on initial date column

Add column to existing table set default to sysdate for existing records and tomorrows date for new entries

How to conditionally add a new column with a new date time based on another column

Add weeks to a date column to create a new date column

Create new Pandas column from existing column using previous date

How to add date column to already existing time column in python?

Add categorical season column to dataframe from existing date column

Making new column based on date with variable year

create a new column based on multiple date entries

Create a new column in a dataframe based on a date

mutate new column based on date interval

Pandas: Add columns to DataFrame based off existing column

Add new column based on existing column with concat values Sprak dataframe

Add a new column to a dataframe based on an existing column value using pandas

Add values to new column based on values of existing column efficiently with Pandas

Add a new column based on string value from an existing column in Rstudio

How to add new column based on existing column in a datatable in VB?