-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Series.map should return default dictionary values rather than NaN #15999
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
Comments
why would you do this? |
I've ran into this issue several times with By using a defaultdict or Counter, the user has chosen that they would like default values. If they don't want defaults, they should just convert or use dict. |
|
looks like you just should do |
Because I have a counter of occurrences that I want to add as a column to a dataframe. In many cases the counter cannot be created in pandas using
Now you could always use
Map does accept a Counter, since it's a subclass of dict, and provides no warning. |
Right now we take a fastpath, building an Line 2137 in 614a48e
So probably either should add a slowpath that respects full semantics if passed a dict subclass, or just raise. |
What about adding the following to the head of the function? if isinstance(arg, (collections.Counter, collections.defaultdict)):
dictionary = arg
arg = lambda x: dictionary[x] Note there are other ways of simplifying the function's code I would also explore. I'm happy to submit a PR and add tests if this is an enhancement that would be accepted. |
* series.map: support dicts with defaults closes #15999
collections.Counter
andcollections.defaultdict
both have default values. However,pandas.Series.map
does not respect these defaults and instead returns missing values.The issue is illustrated below:
Here's the output:
The workaround is rather easy (
lambda x: dictionary[x]
) and shouldn't be to hard to implement. Are people on board with the change? Is there a performance concern with looking up each key independently?The text was updated successfully, but these errors were encountered: