Code: Select all
import polars as pl
data = {"col1": ['2020/01/01', '2020/02/01'], "col2": ['2020/01/01', '2020/02/01']}
df = pl.DataFrame(data, schema={"col1": pl.String, "col2": pl.String})
df = df.with_columns(
pl.col('col1').str.to_datetime(),
pl.col('col2').str.to_datetime()
)
df.with_columns(
pl.col(pl.DATETIME_DTYPES).dt.year()
)
Code: Select all
┌──────┬──────┐
│ col1 ┆ col2 │
│ --- ┆ --- │
│ i32 ┆ i32 │
╞══════╪══════╡
│ 2020 ┆ 2020 │
│ 2020 ┆ 2020 │
└──────┴──────┘
Zum Beispiel, wenn ich dem vorhandenen Namen ein Suffix hinzufügen möchte.
Code: Select all
┌─────────────────────┬─────────────────────┬───────────┬───────────┐
│ col1 ┆ col2 ┆ col1_year ┆ col2_year │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ datetime[μs] ┆ i32 ┆ i32 │
╞═════════════════════╪═════════════════════╪═══════════╪═══════════╡
│ 2020-01-01 00:00:00 ┆ 2020-01-01 00:00:00 ┆ 2020 ┆ 2020 │
│ 2020-02-01 00:00:00 ┆ 2020-02-01 00:00:00 ┆ 2020 ┆ 2020 │
└─────────────────────┴─────────────────────┴───────────┴───────────┘
Mobile version