Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare date values from rows in python polars?

I have a dataframe with date of births as

pl.DataFrame({'idx':[1,2,3,4,5,6],
              'date_of_birth':['03/06/1990','3/06/1990','11/12/2000','01/02/2021','1/02/2021','3/06/1990']})

enter image description here

Here I would like to compare date of birth(Format: Month/Day/Year) of each row and tag yes if the months are equal such as 03 – 3, 01 -1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are dates as 03/06/1900, 3/06/1990, they are generally same. but here they are treated as different. How to figure out these kind scenarios ?

The expected output as:

enter image description here

>Solution :

Let’s convert date_of_birth to datetime then check if the month is satisfied

import numpy as np
import pandas as pd
import polars as pl

s = np.where(pd.to_datetime(df['date_of_birth']).month.isin([3, 1]), 'Yes', 'No')

out = df.with_columns([
    pl.lit(s).alias('flag')
])
print(out)

┌─────┬───────────────┬──────┐
│ idx ┆ date_of_birth ┆ flag │
│ --- ┆ ---           ┆ ---  │
│ i64 ┆ str           ┆ str  │
╞═════╪═══════════════╪══════╡
│ 1   ┆ 03/06/1990    ┆ Yes  │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 2   ┆ 3/06/1990     ┆ Yes  │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 3   ┆ 11/12/2000    ┆ No   │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 4   ┆ 01/02/2021    ┆ Yes  │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 5   ┆ 1/02/2021     ┆ Yes  │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 6   ┆ 3/06/1990     ┆ Yes  │
└─────┴───────────────┴──────┘
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading