2

Taking the example straight from the docs:

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"],
    }
).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?

2
  • 1
    map_dict was added in 0.16.3: what's your version (print(pl.__version__))? Commented Mar 24, 2023 at 10:06
  • @Timus you are correct, I should have checked this. I am using conda not pip so the latest version is not available to me. Thanks! Commented Mar 24, 2023 at 11:11

3 Answers 3

3

map_dict was removed and replaced by 2 new methods.

replace_strict is the one that allows you to specify a default=

df.with_columns(
    pl.col("country_code")
      .replace_strict(country_code_dict, default="unknown")
      .alias("remapped")
)
shape: (4, 3)
┌────────┬──────────────┬───────────────┐
│ row_nr ┆ country_code ┆ remapped      │
│ ---    ┆ ---          ┆ ---           │
│ u32    ┆ str          ┆ str           │
╞════════╪══════════════╪═══════════════╡
│ 0      ┆ FR           ┆ France        │
│ 1      ┆ null         ┆ Not specified │
│ 2      ┆ ES           ┆ unknown       │
│ 3      ┆ DE           ┆ Germany       │
└────────┴──────────────┴───────────────┘
Sign up to request clarification or add additional context in comments.

Comments

0

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  │

└──────────────┴──────────┘

Comments

0

Apply converted inro map_elements https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.map_elements.html

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")
    .map_elements(remap_country_code, return_dtype=pl.Utf8)
    .alias("remapped")
)

print(df)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.