Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cancan101 opened this issue Sep 13, 2016 · 6 comments
Open

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

cancan101 opened this issue Sep 13, 2016 · 6 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@cancan101
Copy link
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.

@jreback
Copy link
Contributor

jreback commented Sep 13, 2016

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

@jreback jreback added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate API Design labels Sep 13, 2016
@cancan101
Copy link
Contributor Author

cancan101 commented Sep 13, 2016

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
Copy link
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).

@cancan101
Copy link
Contributor Author

@bollard
Copy link

bollard commented Dec 5, 2022

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
Copy link

IPerezGonzalez commented Feb 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

6 participants