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

Question:

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?

Asked By: user17033672

||

Answers:

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  │

└──────────────┴──────────┘
Answered By: Abdulmajeed

.map_dict is a helper function built around .join – which you can do manually.

df.join(
   pl.from_dict(country_code_dict).melt(),
   left_on="country_code",
   right_on="variable",
   how="left"
)
shape: (4, 3)
┌────────┬──────────────┬─────────┐
│ row_nr ┆ country_code ┆ value   │
│ ---    ┆ ---          ┆ ---     │
│ u32    ┆ str          ┆ str     │
╞════════╪══════════════╪═════════╡
│ 0      ┆ FR           ┆ France  │
│ 1      ┆ null         ┆ null    │
│ 2      ┆ ES           ┆ null    │
│ 3      ┆ DE           ┆ Germany │
└────────┴──────────────┴─────────┘

I removed the None key from the example as polars does not like it.

country_code_dict = {
   "CA": "Canada",
   "DE": "Germany",
   "FR": "France",
}

df = pl.DataFrame(
   {
      "country_code": ["FR", None, "ES", "DE"],
   }
).with_row_count()
Answered By: jqurious
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.