Question
Get max date column name on polars
I'm trying to get the column name containing the maximum date value in my Polars DataFrame. I found a similar question that was already answered here.
However, in my case, I have many columns, and adding them manually would be tedious. I would like to use column selectors cs.datetime()
and have tried the following:
import polars as pl
from datetime import datetime
import polars.selectors as cs
data = {
"ID" : [1,2,3],
"Values_A" : [datetime(1,1,2),datetime(1,1,3),datetime(1,1,4)],
"Values_B" : [datetime(1,1,4),datetime(1,1,7),datetime(1,1,2)]
}
df = pl.DataFrame(data)
def arg_max_horizontal(*columns: pl.Expr) -> pl.Expr:
return (
pl.concat_list(columns)
.list.arg_max()
.replace_strict({i: col_name for i, col_name in enumerate(columns)})
)
(
df
.with_columns(
Largest=arg_max_horizontal(pl.select(cs.datetime()))
)
)
3 57
3