Skip to content

Option to Raise Exception on Unmapped Value in Series.map #14210

Open
@cancan101

Description

@cancan101
Contributor

Add a flag (or extend na_action) to raise an exception in Series.map if a value is not successfully mapped (ie in the case where arg is a dict.

Activity

jreback

jreback commented on Sep 13, 2016

@jreback
Contributor

I am not sure what the intent / meaning of this parameter. @cancan101 can you give an example?

cancan101

cancan101 commented on Sep 13, 2016

@cancan101
ContributorAuthor

If I were do this:

Series(['a', 'b']).map({'a': 1, 'b': 3})

my return type is of int64 and everything is mapped.
However for this:

Series(['a', 'b ']).map({'a': 1, 'b': 3}) # note space

my return type is floatX and there is a nan. Instead this is a "mistake" and I would have preferred an exception to be raised.

I could assert that the type is int (brittle and just works for ints) or perhaps check all of the unique values in advance to make sure they are a subset of the mapping arg keys (require another pass over data).

TomAugspurger

TomAugspurger commented on Sep 13, 2016

@TomAugspurger
Contributor

Related (but different from) #13473

I'd be in favor of adding a 'raise' option when there's a value in the series that doesn't get mapped.

Does anyone know what the na_action parameter actually does? The docs aren't that clear and I can't figure it out with simple testing. Is this controlling NA values in the original series, or the output? Ideally we'd use the errors={'ignore', 'raise'} keyword we're using elsewhere, and deprecate na_action (if it's doing what I think it is).

bollard

bollard commented on Dec 5, 2022

@bollard

Quite the 6-year necroraise, but little bump that would be really useful to have an na_action="raise" on Series.map

A workaround for the moment is to (re)define __missing__ to raise, i.e.

class WillRaise(dict):
    def __missing__(self, key):
        raise KeyError(key)

mapping = WillRaise({"Foo": 1})
pd.Series(["Foo", "Bar"]).map(mapping) # KeyError: 'Bar'
IPerezGonzalez

IPerezGonzalez commented on Feb 16, 2023

@IPerezGonzalez

Hi all,
I find myself with the same need as @cancan101 some years ago: optionally raise an error when a value does not get mapped. It does not look like this has been implemented yet. Are there any plans?
Irene

johnh2o2

johnh2o2 commented on Aug 21, 2024

@johnh2o2

bump to this! would love a raise na_option

kopytjuk

kopytjuk commented on Mar 7, 2025

@kopytjuk
Contributor

Hey guys, I prepared a PR in #60482, please review if the functionality and docs are understandable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementError ReportingIncorrect or improved errors from pandasMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cancan101@bollard@jreback@TomAugspurger@johnh2o2

        Issue actions

          Option to Raise Exception on Unmapped Value in Series.map · Issue #14210 · pandas-dev/pandas