I am trying to create a new column using apply in polars.
For this, I tried the following operation:
df = df.with_columns(
pl.col("AH_PROC_REALIZADO")
.apply(get_procedure_description)
.alias("proced_descr")
)
But I'm getting the error:
AttributeError: 'Expr' object has no attribute 'apply'
The function I am trying to apply looks as follows.
def get_procedure_description(cod):
proceds = {
'41612': 'CIRURGIA ONCOLOGICA',
'30401': 'RADIOTERAPIA',
...
}
for proced in proceds.keys():
if cod[: len(proced)] == proced:
return proceds[proced]
return None
pl.Expr.apply
was deprecated in favour of pl.Expr.map_elements
in Polars release 0.19.0. Recently, pl.Expr.apply
was removed in the release of Polars 1.0.0.
You can adapt your code to the new version as follows.
df.with_columns(
pl.col("AH_PROC_REALIZADO")
.map_elements(get_procedure_description, return_dtype=pl.String)
.alias("proced_descr")
)