从极坐标中的日期时间列检索日期

问题描述 投票:0回答:2

目前,当我尝试从极坐标日期时间列中检索日期时,我必须写一些东西。类似于:

df = pl.DataFrame({
    'time': [dt.datetime.now()]
})


df = df.select([
    pl.col("*"),
    pl.col("time").apply(lambda x: x.date()).alias("date")
])

有没有不同的方式,更接近:

pl.col("time").dt.date().alias("date")
python dataframe python-polars
2个回答
11
投票

您可以将

Datetime
列转换为
Date
列:


import datetime
import polars as pl

df = pl.DataFrame({
    'time': [datetime.datetime.now()]
})

df.with_columns(
    pl.col("time").cast(pl.Date)
)
shape: (1, 1)
┌────────────┐
│ time       │
│ ---        │
│ date       │
╞════════════╡
│ 2022-08-02 │
└────────────┘


3
投票

自 Polars 版本 0.16.15(几天前发布)开始。您问题中的代码实际上是有效的。

pl.col("time").dt.date().alias("date")

这是使之成为可能的差异。它适用于

pl.Series
pl.Expr
(用于惰性数据帧)。

© www.soinside.com 2019 - 2024. All rights reserved.