我正在尝试将时间戳舍入到极坐标中的下一分钟。
例如:
2023-01-01 10:05:00
应该留下来2023-01-01 10:05:00
2023-01-01 10:05:01
应该是2023-01-01 10:06:00
这适用于带 ceil 的 pandas:
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),
]
}
)
时间戳 |
---|
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')
时间戳 |
---|
2023-01-01T10:05:00.000000 |
2023-01-01T10:06:00.000000 |
2023-01-01T10:06:00.000000 |
2023-01-01T10:07:00.000000 |
我在极地发现的唯一方法如下:
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))
)
您可以使用
polars-xdt
为此
安装:
pip install polars-xdt
用途:
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 │
└─────────────────────┴─────────────────────┘