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:
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))
)
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 │
└─────────────────────┴─────────────────────┘