Skip to content

Commit 6621cb6

Browse files
l736xjorisvandenbossche
authored andcommitted
DOC: Improve the docstrings of CategoricalIndex.map (#20286)
1 parent 17c1fad commit 6621cb6

File tree

4 files changed

+131
-25
lines changed

4 files changed

+131
-25
lines changed

pandas/core/arrays/categorical.py

+60-7
Original file line numberDiff line numberDiff line change
@@ -1080,20 +1080,73 @@ def remove_unused_categories(self, inplace=False):
10801080
return cat
10811081

10821082
def map(self, mapper):
1083-
"""Apply mapper function to its categories (not codes).
1083+
"""
1084+
Map categories using input correspondence (dict, Series, or function).
1085+
1086+
Maps the categories to new categories. If the mapping correspondence is
1087+
one-to-one the result is a :class:`~pandas.Categorical` which has the
1088+
same order property as the original, otherwise a :class:`~pandas.Index`
1089+
is returned.
1090+
1091+
If a `dict` or :class:`~pandas.Series` is used any unmapped category is
1092+
mapped to `NaN`. Note that if this happens an :class:`~pandas.Index`
1093+
will be returned.
10841094
10851095
Parameters
10861096
----------
1087-
mapper : callable
1088-
Function to be applied. When all categories are mapped
1089-
to different categories, the result will be Categorical which has
1090-
the same order property as the original. Otherwise, the result will
1091-
be np.ndarray.
1097+
mapper : function, dict, or Series
1098+
Mapping correspondence.
10921099
10931100
Returns
10941101
-------
1095-
applied : Categorical or Index.
1102+
pandas.Categorical or pandas.Index
1103+
Mapped categorical.
1104+
1105+
See Also
1106+
--------
1107+
CategoricalIndex.map : Apply a mapping correspondence on a
1108+
:class:`~pandas.CategoricalIndex`.
1109+
Index.map : Apply a mapping correspondence on an
1110+
:class:`~pandas.Index`.
1111+
Series.map : Apply a mapping correspondence on a
1112+
:class:`~pandas.Series`.
1113+
Series.apply : Apply more complex functions on a
1114+
:class:`~pandas.Series`.
1115+
1116+
Examples
1117+
--------
1118+
>>> cat = pd.Categorical(['a', 'b', 'c'])
1119+
>>> cat
1120+
[a, b, c]
1121+
Categories (3, object): [a, b, c]
1122+
>>> cat.map(lambda x: x.upper())
1123+
[A, B, C]
1124+
Categories (3, object): [A, B, C]
1125+
>>> cat.map({'a': 'first', 'b': 'second', 'c': 'third'})
1126+
[first, second, third]
1127+
Categories (3, object): [first, second, third]
1128+
1129+
If the mapping is one-to-one the ordering of the categories is
1130+
preserved:
1131+
1132+
>>> cat = pd.Categorical(['a', 'b', 'c'], ordered=True)
1133+
>>> cat
1134+
[a, b, c]
1135+
Categories (3, object): [a < b < c]
1136+
>>> cat.map({'a': 3, 'b': 2, 'c': 1})
1137+
[3, 2, 1]
1138+
Categories (3, int64): [3 < 2 < 1]
1139+
1140+
If the mapping is not one-to-one an :class:`~pandas.Index` is returned:
1141+
1142+
>>> cat.map({'a': 'first', 'b': 'second', 'c': 'first'})
1143+
Index(['first', 'second', 'first'], dtype='object')
1144+
1145+
If a `dict` is used, all unmapped categories are mapped to `NaN` and
1146+
the result is an :class:`~pandas.Index`:
10961147
1148+
>>> cat.map({'a': 'first', 'b': 'second'})
1149+
Index(['first', 'second', nan], dtype='object')
10971150
"""
10981151
new_categories = self.categories.map(mapper)
10991152
try:

pandas/core/indexes/base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -3352,22 +3352,23 @@ def groupby(self, values):
33523352
return result
33533353

33543354
def map(self, mapper, na_action=None):
3355-
"""Map values of Series using input correspondence
3355+
"""
3356+
Map values using input correspondence (a dict, Series, or function).
33563357
33573358
Parameters
33583359
----------
33593360
mapper : function, dict, or Series
3361+
Mapping correspondence.
33603362
na_action : {None, 'ignore'}
33613363
If 'ignore', propagate NA values, without passing them to the
3362-
mapping function
3364+
mapping correspondence.
33633365
33643366
Returns
33653367
-------
33663368
applied : Union[Index, MultiIndex], inferred
33673369
The output of the mapping function applied to the index.
33683370
If the function returns a tuple with more than one element
33693371
a MultiIndex will be returned.
3370-
33713372
"""
33723373

33733374
from .multi import MultiIndex

pandas/core/indexes/category.py

+58-7
Original file line numberDiff line numberDiff line change
@@ -660,20 +660,71 @@ def is_dtype_equal(self, other):
660660
take_nd = take
661661

662662
def map(self, mapper):
663-
"""Apply mapper function to its categories (not codes).
663+
"""
664+
Map values using input correspondence (a dict, Series, or function).
665+
666+
Maps the values (their categories, not the codes) of the index to new
667+
categories. If the mapping correspondence is one-to-one the result is a
668+
:class:`~pandas.CategoricalIndex` which has the same order property as
669+
the original, otherwise an :class:`~pandas.Index` is returned.
670+
671+
If a `dict` or :class:`~pandas.Series` is used any unmapped category is
672+
mapped to `NaN`. Note that if this happens an :class:`~pandas.Index`
673+
will be returned.
664674
665675
Parameters
666676
----------
667-
mapper : callable
668-
Function to be applied. When all categories are mapped
669-
to different categories, the result will be a CategoricalIndex
670-
which has the same order property as the original. Otherwise,
671-
the result will be a Index.
677+
mapper : function, dict, or Series
678+
Mapping correspondence.
672679
673680
Returns
674681
-------
675-
applied : CategoricalIndex or Index
682+
pandas.CategoricalIndex or pandas.Index
683+
Mapped index.
684+
685+
See Also
686+
--------
687+
Index.map : Apply a mapping correspondence on an
688+
:class:`~pandas.Index`.
689+
Series.map : Apply a mapping correspondence on a
690+
:class:`~pandas.Series`.
691+
Series.apply : Apply more complex functions on a
692+
:class:`~pandas.Series`.
676693
694+
Examples
695+
--------
696+
>>> idx = pd.CategoricalIndex(['a', 'b', 'c'])
697+
>>> idx
698+
CategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],
699+
ordered=False, dtype='category')
700+
>>> idx.map(lambda x: x.upper())
701+
CategoricalIndex(['A', 'B', 'C'], categories=['A', 'B', 'C'],
702+
ordered=False, dtype='category')
703+
>>> idx.map({'a': 'first', 'b': 'second', 'c': 'third'})
704+
CategoricalIndex(['first', 'second', 'third'], categories=['first',
705+
'second', 'third'], ordered=False, dtype='category')
706+
707+
If the mapping is one-to-one the ordering of the categories is
708+
preserved:
709+
710+
>>> idx = pd.CategoricalIndex(['a', 'b', 'c'], ordered=True)
711+
>>> idx
712+
CategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],
713+
ordered=True, dtype='category')
714+
>>> idx.map({'a': 3, 'b': 2, 'c': 1})
715+
CategoricalIndex([3, 2, 1], categories=[3, 2, 1], ordered=True,
716+
dtype='category')
717+
718+
If the mapping is not one-to-one an :class:`~pandas.Index` is returned:
719+
720+
>>> idx.map({'a': 'first', 'b': 'second', 'c': 'first'})
721+
Index(['first', 'second', 'first'], dtype='object')
722+
723+
If a `dict` is used, all unmapped categories are mapped to `NaN` and
724+
the result is an :class:`~pandas.Index`:
725+
726+
>>> idx.map({'a': 'first', 'b': 'second'})
727+
Index(['first', 'second', nan], dtype='object')
677728
"""
678729
return self._shallow_copy_with_infer(self.values.map(mapper))
679730

pandas/core/series.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -2831,25 +2831,26 @@ def unstack(self, level=-1, fill_value=None):
28312831

28322832
def map(self, arg, na_action=None):
28332833
"""
2834-
Map values of Series using input correspondence (which can be
2835-
a dict, Series, or function)
2834+
Map values of Series using input correspondence (a dict, Series, or
2835+
function).
28362836
28372837
Parameters
28382838
----------
28392839
arg : function, dict, or Series
2840+
Mapping correspondence.
28402841
na_action : {None, 'ignore'}
28412842
If 'ignore', propagate NA values, without passing them to the
2842-
mapping function
2843+
mapping correspondence.
28432844
28442845
Returns
28452846
-------
28462847
y : Series
2847-
same index as caller
2848+
Same index as caller.
28482849
28492850
Examples
28502851
--------
28512852
2852-
Map inputs to outputs (both of type `Series`)
2853+
Map inputs to outputs (both of type `Series`):
28532854
28542855
>>> x = pd.Series([1,2,3], index=['one', 'two', 'three'])
28552856
>>> x
@@ -2900,9 +2901,9 @@ def map(self, arg, na_action=None):
29002901
29012902
See Also
29022903
--------
2903-
Series.apply: For applying more complex functions on a Series
2904-
DataFrame.apply: Apply a function row-/column-wise
2905-
DataFrame.applymap: Apply a function elementwise on a whole DataFrame
2904+
Series.apply : For applying more complex functions on a Series.
2905+
DataFrame.apply : Apply a function row-/column-wise.
2906+
DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
29062907
29072908
Notes
29082909
-----

0 commit comments

Comments
 (0)