-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Support dicts with default values in series.map #16002
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e8f9d27
TST: test series.map on Counter & defaultdict
dhimmel 96d12a6
series.map: support dicts with defaults
dhimmel 961ea46
Detect default support using __missing__
dhimmel d73cee8
TST: dict subclasses with/out __missing__
dhimmel 2a2bab7
Add v0.20.0 what's new
dhimmel 4f3dc6b
DOC: comment series.map
dhimmel 24e1478
DOC: remove unnecessary lambda in formatter
dhimmel 11f5769
DOC: add counter example to docstring
dhimmel ddb0480
DOC: Address review comments
dhimmel 1f56c81
DOC: mention GitHub issue in test docstring
dhimmel 79cfd11
DOC: move missing dict key info to notes
dhimmel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2079,8 +2079,8 @@ def map(self, arg, na_action=None): | |
two bar | ||
three baz | ||
|
||
Mapping a dictionary keys on the index labels works similar as | ||
with a `Series`: | ||
If `arg` is a dictionary, return a new Series with values converted | ||
according to the dictionary's mapping: | ||
|
||
>>> z = {1: 'A', 2: 'B', 3: 'C'} | ||
|
||
|
@@ -2094,16 +2094,14 @@ def map(self, arg, na_action=None): | |
|
||
>>> s = pd.Series([1, 2, 3, np.nan]) | ||
|
||
>>> s2 = s.map(lambda x: 'this is a string {}'.format(x), | ||
na_action=None) | ||
>>> s2 = s.map('this is a string {}'.format, na_action=None) | ||
0 this is a string 1.0 | ||
1 this is a string 2.0 | ||
2 this is a string 3.0 | ||
3 this is a string nan | ||
dtype: object | ||
|
||
>>> s3 = s.map(lambda x: 'this is a string {}'.format(x), | ||
na_action='ignore') | ||
>>> s3 = s.map('this is a string {}'.format, na_action='ignore') | ||
0 this is a string 1.0 | ||
1 this is a string 2.0 | ||
2 this is a string 3.0 | ||
|
@@ -2115,6 +2113,23 @@ def map(self, arg, na_action=None): | |
Series.apply: For applying more complex functions on a Series | ||
DataFrame.apply: Apply a function row-/column-wise | ||
DataFrame.applymap: Apply a function elementwise on a whole DataFrame | ||
|
||
Notes | ||
----- | ||
When `arg` is a dictionary, values in Series that are not in the | ||
dictionary (as keys) are converted to ``NaN``. However, if the | ||
dictionary is a ``dict`` subclass that defines ``__missing__`` (i.e. | ||
provides a method for default values), then this default is used | ||
rather than ``NaN``: | ||
|
||
>>> from collections import Counter | ||
>>> counter = Counter() | ||
>>> counter['bar'] += 1 | ||
>>> y.map(counter) | ||
1 0 | ||
2 1 | ||
3 0 | ||
dtype: int64 | ||
""" | ||
|
||
if is_extension_type(self.dtype): | ||
|
@@ -2132,13 +2147,23 @@ def map_f(values, f): | |
else: | ||
map_f = lib.map_infer | ||
|
||
if isinstance(arg, (dict, Series)): | ||
if isinstance(arg, dict): | ||
if isinstance(arg, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a 1-line comment here on what you are doing |
||
if hasattr(arg, '__missing__'): | ||
# If a dictionary subclass defines a default value method, | ||
# convert arg to a lookup function (GH #15999). | ||
dict_with_default = arg | ||
arg = lambda x: dict_with_default[x] | ||
else: | ||
# Dictionary does not have a default. Thus it's safe to | ||
# convert to an indexed series for efficiency. | ||
arg = self._constructor(arg, index=arg.keys()) | ||
|
||
if isinstance(arg, Series): | ||
# arg is a Series | ||
indexer = arg.index.get_indexer(values) | ||
new_values = algorithms.take_1d(arg._values, indexer) | ||
else: | ||
# arg is a function | ||
new_values = map_f(values, arg) | ||
|
||
return self._constructor(new_values, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@jreback let me know if you want me to revert the deletion of these blank lines.