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

Equivalent of pandas.Series.dt.ceil in polars

I’m trying to round timestamp to the next minutes in polars.

For example:

  • 2023-01-01 10:05:00 should stay 2023-01-01 10:05:00
  • 2023-01-01 10:05:01 should be 2023-01-01 10:06:00

This works in pandas with ceil:

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

import polars as pl
import datetime

df = pl.DataFrame(
    {'timestamp' :[
        datetime.datetime(2023, 1, 1, 10, 5, 0),
        datetime.datetime(2023, 1, 1, 10, 5, 30),
        datetime.datetime(2023, 1, 1, 10, 6, 0),
        datetime.datetime(2023, 1, 1, 10, 6, 1),
    ]
    }
)
timestamp
2023-01-01T10:05:00.000000
2023-01-01T10:05:30.000000
2023-01-01T10:06:00.000000
2023-01-01T10:06:01.000000
df['timestamp'].to_pandas().dt.ceil('1min')
timestamp
2023-01-01T10:05:00.000000
2023-01-01T10:06:00.000000
2023-01-01T10:06:00.000000
2023-01-01T10:07:00.000000

The only way I found in polars is the following:

df.with_columns(
    pl.when(pl.col('timestamp').dt.truncate('1m') == pl.col('timestamp'))
    .then(pl.col('timestamp'))
    .otherwise(pl.col('timestamp').dt.truncate('1m') + datetime.timedelta(minutes=1))
)

>Solution :

you can use polars-xdt for this

installation:

pip install polars-xdt

usage:

import polars_xdt as xdt

df.with_columns(timestamp_ceil = xdt.ceil('timestamp', '1m'))
shape: (4, 2)
┌─────────────────────┬─────────────────────┐
│ timestamp           ┆ timestamp_ceil      │
│ ---                 ┆ ---                 │
│ datetime[μs]        ┆ datetime[μs]        │
╞═════════════════════╪═════════════════════╡
│ 2023-01-01 10:05:00 ┆ 2023-01-01 10:05:00 │
│ 2023-01-01 10:05:30 ┆ 2023-01-01 10:06:00 │
│ 2023-01-01 10:06:00 ┆ 2023-01-01 10:06:00 │
│ 2023-01-01 10:06:01 ┆ 2023-01-01 10:07:00 │
└─────────────────────┴─────────────────────┘
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