Question

Cast columns that might not exist in Polars

I want to cast a column to another type, but there is a possibility that the column does not exist in the df.

import polars as pl
import polars.selectors as cs

# Sample DataFrame
data = {
    "viewCount": [100, 200, 300],
    "likeCount": [10, 20, 30],
    "id": [1, 2, 3]
}

df = pl.DataFrame(data)

# Columns we want to cast
columns_to_cast = ["viewCount", "likeCount", "favoriteCount", "id", "does_not_exist"]

# Check for existence and cast if the column exists
for col in columns_to_cast:
    if col in df.columns:
        df = df.with_columns(pl.col(col).cast(pl.Float64))

print(df)

Can the be achieved using polars' expression API?

 2  37  2
1 Jan 1970

Solution

 1

You can use column selectors to select columns by name with pl.selectors.by_name and pass require_all=False.

import polars.selectors as cs

df.with_columns(
    cs.by_name(columns_to_cast, require_all=False).cast(pl.Float64)
)
shape: (3, 3)
┌───────────┬───────────┬─────┐
│ viewCount ┆ likeCount ┆ id  │
│ ---       ┆ ---       ┆ --- │
│ f64       ┆ f64       ┆ f64 │
╞═══════════╪═══════════╪═════╡
│ 100.0     ┆ 10.0      ┆ 1.0 │
│ 200.0     ┆ 20.0      ┆ 2.0 │
│ 300.0     ┆ 30.0      ┆ 3.0 │
└───────────┴───────────┴─────┘
2024-07-25
Hericks