Consider a Polars data frame with a column of str
type that indicates the date in the format '27 July 2020'
. I would like to convert this column to the polars.datetime
type, which is distinct from the Python standard datetime
. The following code, using the standard datetime
format, works but Polars does not recognise the values in the column as dates.
import polars as pl
from datetime import datetime
df = pd.read_csv('<some CSV file containing a column called 'event_date'>')
df = df.with_columns([
pl.col('event_date').apply(lambda x: x.replace(" ","-"))\
.apply(lambda x: datetime.strptime(x, '%d-%B-%Y'))
])
Suppose we try to process df
further to create a new column indicating the quarter of the year an event took place.
df = df.with_columns([
pl.col('event_date').apply(lambda x: x.month)\
.apply(lambda x: 1 if x in range(1,4) else 2 if x in range(4,7) else 3 if x in range(7,10) else 4)\
.alias('quarter')
])
The code returns the following error because it qualifies event_type
as dtype Object("object")
and not as datetime
or polars.datetime
thread '<unnamed>' panicked at 'dtype Object("object") not supported', src/series.rs:992:24
--- PyO3 is resuming a panic after fetching a PanicException from Python. ---
PanicException: Unwrapped panic from Python code