-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Series map ignoring na_action for dict or series mapper #47585
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
Conversation
@@ -840,6 +840,10 @@ def _map_values(self, mapper, na_action=None): | |||
f"{na_action} was passed" | |||
) | |||
raise ValueError(msg) | |||
|
|||
if na_action == "ignore": | |||
mapper = mapper[mapper.index.notna()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since there is quite a few paths through here, might be worth adding tests for categorical Series and also using a mapping that doesn't get converted to a series above.
I was quite happy to not treat this as a bug and just update the docs, see #46588 (comment) and #46588 (comment)
but it appears that @rhshadrach is happy to consider it a bug #47527 (comment)
so if we are honoring the na_action
for types other than callables, it should probably be tested for the default dict case too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! and change the docstring too.
without passing them to the mapping function -> without passing them to the mapping`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I would be ok with treating this ok as is, what do you think @rhshadrach ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm also happy to treat it as a bug, but also consider it an enhancement so that we have a few more tests than a regular bugfix. (although we might already test)
I assume (not tried) that the default dict case goes through as a function and already honors the na_action
and therefore no extra code needed. This also would be a good argument for treating it as a bug since the default dict case and regular dict are currently inconsistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm good point, but defaultdict seems to be off
mapping = defaultdict(int, {1: 10, np.nan: 42})
ser = Series([1, np.nan, 2])
result = ser.map(mapping)
This should return Series([10, 42, x])
where x is None? 2? Not sure, but this returns
Series([10, 0, 0])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm +1 on either option here - fixing the behavior to match the current docs or changing the docs as @simonjayhawkins has suggested. I'd lean toward the latter, but not strongly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should return
Series([10, 42, x])
where x is None? 2? Not sure, but this returns
Series([10, 0, 0])
The second 0
(index 2) looks correct, but index 1 returning 0
too looks incorrect.
defaultdict(int, {1: 10, np.nan: 42})[2]
# 0
int()
# 0
defaultdict(int, {1: 10, np.nan: 42})[np.nan]
# 42
I'm +1 on either option here - fixing the behavior to match the current docs or changing the docs as @simonjayhawkins has suggested. I'd lean toward the latter, but not strongly.
Ignore for a minute the latent bug identified above, when I made those comments I didn't appreciate that a defaultdict
would take the same path as a callable and hence already takes account of the na_action
kwarg.
mapping = {1: 10, np.nan: 42}
print(Series([1, np.nan, 2]).map(mapping, na_action="ignore"))
# 0 10.0
# 1 42.0
# 2 NaN
# dtype: float64
mapping = defaultdict(int, {1: 10, np.nan: 42})
print(Series([1, np.nan, 2]).map(mapping, na_action="ignore"))
# 0 10.0
# 1 NaN
# 2 0.0
# dtype: float64
So for consistency, i'm now favoring the the first option. (i.e generally happy with this PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with the first zero (at index 1) in #47585 (comment) appears to be due to using np.nan in a dictionary:
ser = Series([1, np.nan, 2])
print(id(ser._values[1]), id(np.nan))
# 139702525343536 139704452752816
Because one gets a view of np.nan, the ids are different and therefore lookup fails. This is "expected" behavior when working with np.nan in a dictionary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this makes sense, thanks very much. I’ll add additional tests and then this should be ready I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Please don’t merge yet, this needs more tests |
@phofl - if you convert to draft, it can't be merged. |
Tests added, ready now |
Thanks @phofl |
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.