0

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?

Areza
  • 5,623
  • 7
  • 48
  • 79
nosewitz
  • 1
  • 1

2 Answers2

1

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", name="drange")
months = dates.dt.month()
Scout
  • 27
  • 5
1

If the column is already in the polars pl.Datetime format:

df = (
     df
    .with_column(pl.col("column").dt.strftime("%b").alias("month"))
    )