Search code examples
pythonpython-polars

AttributeError: 'Expr' object has no attribute 'map_dict' polars Document example error


Taking the example straight from the docs:

country_code_dict = {
    "CA": "Canada",
    "DE": "Germany",
    "FR": "France",
    None: "Not specified",
}
df = pl.DataFrame(
    {
        "country_code": ["FR", None, "ES", "DE"],
    }
).with_row_count()

df.with_columns(
    pl.col("country_code")
    .map_dict(country_code_dict, default="unknown")
    .alias("remapped")
)

here: https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.Expr.map_dict.html

Gives the error: AttributeError: 'Expr' object has no attribute 'map_dict'

Is there another way to do this mapping operation?


Solution

  • you can use the apply function instead of the map_dict function to achieve the mapping operation

        import polars as pl
    
    country_code_dict = {
        "CA": "Canada",
        "DE": "Germany",
        "FR": "France",
        None: "Not specified",
    }
    
    df = pl.DataFrame(
        {
            "country_code": ["FR", None, "ES", "DE"],
        }
    )
    
    def remap_country_code(code):
        if code is None:
            return country_code_dict[None]
        return country_code_dict.get(code, "unknown")
    
    df = df.with_columns(
        pl.col("country_code")
        .apply(remap_country_code, return_dtype=pl.Utf8)
        .alias("remapped")
    )
    
    print(df)
    

    Output:

    shape: (4, 2)
    
        ┌──────────────┬──────────┐
        │ country_code ┆ remapped │
        │ ---          ┆ ---      │
        │ str          ┆ str      │
        ╞══════════════╪══════════╡
        │ FR           ┆ France   │
        │ null         ┆ null     │
        │ ES           ┆ unknown  │
        │ DE           ┆ Germany  │
    
    └──────────────┴──────────┘