Question
Using Polars with Python and being thrown the following exception: AttributeError: 'Expr' object has no attribute 'apply'
I am trying to apply a function to a Dataframe column (series) that retrieves the day of the week based on the timestamps in the column. However, I am being thrown the following exception, even though the Polars docs include documentation for polars.Expr.apply
.
AttributeError: 'Expr' object has no attribute 'apply'.
My goal is to create a new column of day names using the following code where the alertTime
column is of dtype datetime64
:
def get_day(dt_obj):
days_of_week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
return days_of_week[dt_obj.weekday()]
# Get the day of the week from the timestamp
df = df.with_columns(
pl.col('alertTime').apply(get_day, return_dtype=pl.Utf8).alias('day_of_week')
)
Could anyone help with where I might be going wrong?