Reputation: 1
I am trying to convert an existing column in my polars dataframe from Date to Month. The documentation here is not clear to me on how to call such methods.
In pandas it looks like this to convert Date -> Month:
pd.DatetimeIndex(df['column']).month.astype(np.int16)
What is the polars equivalent?
Upvotes: 0
Views: 3989
Reputation: 57
You need to create a dt
object first before applying .month()
from datetime import date
dates = pl.date_range(date(2022, 1, 1), date(2022, 3, 1), "1d")
months = dates.dt.month()
Upvotes: 1
Reputation: 11
If the column
is already in the polars pl.Datetime
format:
df = (
df
.with_columns(pl.col("column").dt.strftime("%b").alias("month"))
)
Upvotes: 1