Is there any built-in function in polars
or a better way to convert time durations to numeric by defining the time resolution (e.g.: days, hours, minutes)?
# Create a dataframe
df = pl.DataFrame(
{
"from": ["2023-01-01", "2023-01-02", "2023-01-03"],
"to": ["2023-01-04", "2023-01-05", "2023-01-06"],
}
)
# Convert to date and calculate the time difference
df = df.with_columns(
[
pl.col("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_date"),
]
).with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
# Convert the time difference to int (in days)
df = df.with_columns(
((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
)