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(
1551
1551
Series.apply : Apply more complex functions on a
1552
1552
:class:`~pandas.Series`.
1553
1553
1554
+ Notes
1555
+ -----
1556
+ The mapping function is applied to the categories, not to
1557
+ each element of the array.
1558
+
1554
1559
Examples
1555
1560
--------
1556
1561
>>> cat = pd.Categorical(["a", "b", "c"])
Original file line number Diff line number Diff line change @@ -4389,6 +4389,12 @@ def map(
4389
4389
provides a method for default values), then this default is used
4390
4390
rather than ``NaN``.
4391
4391
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
+
4392
4398
Examples
4393
4399
--------
4394
4400
>>> s = pd.Series(["cat", "dog", np.nan, "rabbit"])
@@ -4428,6 +4434,34 @@ def map(
4428
4434
2 NaN
4429
4435
3 I am a rabbit
4430
4436
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
4431
4465
"""
4432
4466
if func is None :
4433
4467
if "arg" in kwargs :
You can’t perform that action at this time.
0 commit comments