I am trying to create additional columns from an existing column name in polars. The existing column name is starttime. This column contains datetime.
starttime | endtime | storageid | volume_id | avgiops |
---|---|---|---|---|
2022-02-10 09:32:20 | 2022-02-10 09:34:28 | TUNYKYPG72 | 4c8d6c31 | 27 |
2022-02-10 10:34:10 | 2022-02-10 10:35:12 | TUNYKYPG42 | 4c8d6d31 | 34 |
From this table , I would like to create additional columns like weekofyear,dayofweek,year etc.
pl_df.with_column(pl.col('starttime').str.strptime(pl.Date, fmt='%Y').cast(pl.Datetime)).alias
("year")
But it fails as
exceptions.ComputeError: strict conversion to dates failed, maybe set strict=False
In pyspark ,we can create it like below
df_dates = pl.select(
[
weekofyear("starttime").alias("week"),
dayofweek("starttime").alias("weekday"),
hour("starttime").alias("hour"),
dayofmonth("starttime").alias("day"),
to_date("starttime").alias("collectiontime"),
starttime,endtime,storageid,volume_id,avgiops])
How to create additional columns weekofyear,dayofweek,month in polars?
CodePudding user response:
You can .strptime(pl.Datetime)
then use the various methods in the .dt
namespace.
df.with_columns(
pl.col("starttime").str.strptime(pl.Datetime)
).with_columns([
pl.col("starttime").dt.week().alias("week"),
pl.col("starttime").dt.weekday().alias("weekday"),
pl.col("starttime").dt.hour().alias("hour"),
pl.col("starttime").dt.day().alias("day"),
])
shape: (2, 9)
┌─────────────────────┬─────────────────────┬────────────┬───────────┬─────────┬──────┬─────────┬──────┬─────┐
│ starttime | endtime | storageid | volume_id | avgiops | week | weekday | hour | day │
│ --- | --- | --- | --- | --- | --- | --- | --- | --- │
│ datetime[μs] | str | str | str | i64 | u32 | u32 | u32 | u32 │
╞═════════════════════╪═════════════════════╪════════════╪═══════════╪═════════╪══════╪═════════╪══════╪═════╡
│ 2022-02-10 09:32:20 | 2022-02-10 09:34:28 | TUNYKYPG72 | 4c8d6c31 | 27 | 6 | 4 | 9 | 10 │
├─────────────────────┼─────────────────────┼────────────┼───────────┼─────────┼──────┼─────────┼──────┼─────┤
│ 2022-02-10 10:34:10 | 2022-02-10 10:35:12 | TUNYKYPG42 | 4c8d6d31 | 34 | 6 | 4 | 10 | 10 │
└─────────────────────┴─────────────────────┴────────────┴───────────┴─────────┴──────┴─────────┴──────┴─────┘