File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -1551,6 +1551,11 @@ def map(
15511551 Series.apply : Apply more complex functions on a
15521552 :class:`~pandas.Series`.
15531553
1554+ Notes
1555+ -----
1556+ The mapping function is applied to the categories, not to
1557+ each element of the array.
1558+
15541559 Examples
15551560 --------
15561561 >>> cat = pd.Categorical(["a", "b", "c"])
Original file line number Diff line number Diff line change @@ -4389,6 +4389,12 @@ def map(
43894389 provides a method for default values), then this default is used
43904390 rather than ``NaN``.
43914391
4392+ When the Series has ``dtype="category"``, the function is applied
4393+ to the categories and not to each individual value. This means
4394+ that if the same category appears multiple times, the function is
4395+ only called once for that category, and the result is reused for
4396+ all occurrences. Missing values (NaN) are not passed to the function.
4397+
43924398 Examples
43934399 --------
43944400 >>> s = pd.Series(["cat", "dog", np.nan, "rabbit"])
@@ -4428,6 +4434,34 @@ def map(
44284434 2 NaN
44294435 3 I am a rabbit
44304436 dtype: object
4437+
4438+ For categorical data, the function is only applied to the categories:
4439+
4440+ >>> s = pd.Series(list("cabaa"))
4441+ >>> s.map(print)
4442+ c
4443+ a
4444+ b
4445+ a
4446+ a
4447+ 0 None
4448+ 1 None
4449+ 2 None
4450+ 3 None
4451+ 4 None
4452+ dtype: object
4453+
4454+ >>> s_cat = s.astype("category")
4455+ >>> s_cat.map(print) # function called once per unique category
4456+ a
4457+ b
4458+ c
4459+ 0 None
4460+ 1 None
4461+ 2 None
4462+ 3 None
4463+ 4 None
4464+ dtype: object
44314465 """
44324466 if func is None :
44334467 if "arg" in kwargs :
You can’t perform that action at this time.
0 commit comments