在下面的演示代码中,我想向数据框中添加一个新列,该数据框将另一列从
str
转换为具有指定格式的 datetime
。
但是,根据输出,没有任何变化。转换结果不会添加
datetime
类型的新列。
为什么对原始数据框的就地更改不起作用?
data = {
'Timestamp': [
"4/7/2024 18:42:10",
"4/7/2024 18:42:11",
"4/7/2024 18:42:12"
],
'Date': [
"3/19/2024",
"3/19/2024",
"3/19/2024"
],
'Amount': [
"1292.23",
"543.95",
"679.82"
]
}
df = pl.from_dict(data)
print(df, '\n')
df.with_columns(
pl.col('Timestamp')
.str.strptime(pl.Datetime, "%m/%d/%Y %H:%M:%S")
.alias('dt_f')
)
print(df)
shape: (3, 3)
┌───────────────────┬───────────┬─────────┐
│ Timestamp ┆ Date ┆ Amount │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════════════╪═══════════╪═════════╡
│ 4/7/2024 18:42:10 ┆ 3/19/2024 ┆ 1292.23 │
│ 4/7/2024 18:42:11 ┆ 3/19/2024 ┆ 543.95 │
│ 4/7/2024 18:42:12 ┆ 3/19/2024 ┆ 679.82 │
└───────────────────┴───────────┴─────────┘
shape: (3, 3)
┌───────────────────┬───────────┬─────────┐
│ Timestamp ┆ Date ┆ Amount │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════════════╪═══════════╪═════════╡
│ 4/7/2024 18:42:10 ┆ 3/19/2024 ┆ 1292.23 │
│ 4/7/2024 18:42:11 ┆ 3/19/2024 ┆ 543.95 │
│ 4/7/2024 18:42:12 ┆ 3/19/2024 ┆ 679.82 │
└───────────────────┴───────────┴─────────┘
df_formated = df.with_columns(
pl.col('Timestamp')
.str.strptime(pl.Datetime, "%m/%d/%Y %H:%M:%S")
.alias('dt_f')
)
print(df_formated)
shape: (3, 4)
┌───────────────────┬───────────┬─────────┬─────────────────────┐
│ Timestamp ┆ Date ┆ Amount ┆ dt_f │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ datetime[μs] │
╞═══════════════════╪═══════════╪═════════╪═════════════════════╡
│ 4/7/2024 18:42:10 ┆ 3/19/2024 ┆ 1292.23 ┆ 2024-04-07 18:42:10 │
│ 4/7/2024 18:42:11 ┆ 3/19/2024 ┆ 543.95 ┆ 2024-04-07 18:42:11 │
│ 4/7/2024 18:42:12 ┆ 3/19/2024 ┆ 679.82 ┆ 2024-04-07 18:42:12 │
└───────────────────┴───────────┴─────────┴─────────────────────┘
在您的代码中,您使用
df.with_columns()
添加新列。但是,因为您没有将 df.with_columns()
的结果分配回 df
,所以数据框 df
在操作后保持不变。您需要做的是以下操作。
import polars as pl
data = {
'Timestamp': [
"4/7/2024 18:42:10",
"4/7/2024 18:42:11",
"4/7/2024 18:42:12"
],
'Date': [
"3/19/2024",
"3/19/2024",
"3/19/2024"
],
'Amount': [
"1292.23",
"543.95",
"679.82"
]
}
df = pl.from_dict(data)
print("Original DataFrame:")
print(df, '\n')
df = df.with_columns(
pl.col('Timestamp')
.str.strptime(pl.Datetime, "%m/%d/%Y %H:%M:%S")
.alias('dt_f')
)
print("Modified DataFrame:")
print(df)
所以
Original DataFrame:
shape: (3, 3)
┌───────────────────┬───────────┬─────────┐
│ Timestamp ┆ Date ┆ Amount │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════════════╪═══════════╪═════════╡
│ 4/7/2024 18:42:10 ┆ 3/19/2024 ┆ 1292.23 │
│ 4/7/2024 18:42:11 ┆ 3/19/2024 ┆ 543.95 │
│ 4/7/2024 18:42:12 ┆ 3/19/2024 ┆ 679.82 │
└───────────────────┴───────────┴─────────┘
Modified DataFrame:
shape: (3, 4)
┌───────────────────┬───────────┬─────────┬─────────────────────┐
│ Timestamp ┆ Date ┆ Amount ┆ dt_f │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ datetime[μs] │
╞═══════════════════╪═══════════╪═════════╪═════════════════════╡
│ 4/7/2024 18:42:10 ┆ 3/19/2024 ┆ 1292.23 ┆ 2024-04-07 18:42:10 │
│ 4/7/2024 18:42:11 ┆ 3/19/2024 ┆ 543.95 ┆ 2024-04-07 18:42:11 │
│ 4/7/2024 18:42:12 ┆ 3/19/2024 ┆ 679.82 ┆ 2024-04-07 18:42:12 │
└───────────────────┴───────────┴─────────┴─────────────────────┘