@@ -2951,74 +2951,25 @@ def unstack(self, level=-1, fill_value=None):
2951
2951
2952
2952
def map (self , arg , na_action = None ):
2953
2953
"""
2954
- Map values of Series using input correspondence (a dict, Series, or
2955
- function).
2954
+ Map values of Series according to input correspondence.
2955
+
2956
+ Used for substituting each value in a Series with another value,
2957
+ that may be derived from a function, a ``dict`` or
2958
+ a :class:`Series`.
2956
2959
2957
2960
Parameters
2958
2961
----------
2959
2962
arg : function, dict, or Series
2960
2963
Mapping correspondence.
2961
- na_action : {None, 'ignore'}
2962
- If 'ignore', propagate NA values, without passing them to the
2964
+ na_action : {None, 'ignore'}, default None
2965
+ If 'ignore', propagate NaN values, without passing them to the
2963
2966
mapping correspondence.
2964
2967
2965
2968
Returns
2966
2969
-------
2967
- y : Series
2970
+ Series
2968
2971
Same index as caller.
2969
2972
2970
- Examples
2971
- --------
2972
-
2973
- Map inputs to outputs (both of type `Series`):
2974
-
2975
- >>> x = pd.Series([1,2,3], index=['one', 'two', 'three'])
2976
- >>> x
2977
- one 1
2978
- two 2
2979
- three 3
2980
- dtype: int64
2981
-
2982
- >>> y = pd.Series(['foo', 'bar', 'baz'], index=[1,2,3])
2983
- >>> y
2984
- 1 foo
2985
- 2 bar
2986
- 3 baz
2987
-
2988
- >>> x.map(y)
2989
- one foo
2990
- two bar
2991
- three baz
2992
-
2993
- If `arg` is a dictionary, return a new Series with values converted
2994
- according to the dictionary's mapping:
2995
-
2996
- >>> z = {1: 'A', 2: 'B', 3: 'C'}
2997
-
2998
- >>> x.map(z)
2999
- one A
3000
- two B
3001
- three C
3002
-
3003
- Use na_action to control whether NA values are affected by the mapping
3004
- function.
3005
-
3006
- >>> s = pd.Series([1, 2, 3, np.nan])
3007
-
3008
- >>> s2 = s.map('this is a string {}'.format, na_action=None)
3009
- 0 this is a string 1.0
3010
- 1 this is a string 2.0
3011
- 2 this is a string 3.0
3012
- 3 this is a string nan
3013
- dtype: object
3014
-
3015
- >>> s3 = s.map('this is a string {}'.format, na_action='ignore')
3016
- 0 this is a string 1.0
3017
- 1 this is a string 2.0
3018
- 2 this is a string 3.0
3019
- 3 NaN
3020
- dtype: object
3021
-
3022
2973
See Also
3023
2974
--------
3024
2975
Series.apply : For applying more complex functions on a Series.
@@ -3027,20 +2978,51 @@ def map(self, arg, na_action=None):
3027
2978
3028
2979
Notes
3029
2980
-----
3030
- When `arg` is a dictionary, values in Series that are not in the
2981
+ When `` arg` ` is a dictionary, values in Series that are not in the
3031
2982
dictionary (as keys) are converted to ``NaN``. However, if the
3032
2983
dictionary is a ``dict`` subclass that defines ``__missing__`` (i.e.
3033
2984
provides a method for default values), then this default is used
3034
- rather than ``NaN``:
3035
-
3036
- >>> from collections import Counter
3037
- >>> counter = Counter()
3038
- >>> counter['bar'] += 1
3039
- >>> y.map(counter)
3040
- 1 0
3041
- 2 1
3042
- 3 0
3043
- dtype: int64
2985
+ rather than ``NaN``.
2986
+
2987
+ Examples
2988
+ --------
2989
+ >>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
2990
+ >>> s
2991
+ 0 cat
2992
+ 1 dog
2993
+ 2 NaN
2994
+ 3 rabbit
2995
+ dtype: object
2996
+
2997
+ ``map`` accepts a ``dict`` or a ``Series``. Values that are not found
2998
+ in the ``dict`` are converted to ``NaN``, unless the dict has a default
2999
+ value (e.g. ``defaultdict``):
3000
+
3001
+ >>> s.map({'cat': 'kitten', 'dog': 'puppy'})
3002
+ 0 kitten
3003
+ 1 puppy
3004
+ 2 NaN
3005
+ 3 NaN
3006
+ dtype: object
3007
+
3008
+ It also accepts a function:
3009
+
3010
+ >>> s.map('I am a {}'.format)
3011
+ 0 I am a cat
3012
+ 1 I am a dog
3013
+ 2 I am a nan
3014
+ 3 I am a rabbit
3015
+ dtype: object
3016
+
3017
+ To avoid applying the function to missing values (and keep them as
3018
+ ``NaN``) ``na_action='ignore'`` can be used:
3019
+
3020
+ >>> s.map('I am a {}'.format, na_action='ignore')
3021
+ 0 I am a cat
3022
+ 1 I am a dog
3023
+ 2 NaN
3024
+ 3 I am a rabbit
3025
+ dtype: object
3044
3026
"""
3045
3027
new_values = super (Series , self )._map_values (
3046
3028
arg , na_action = na_action )
0 commit comments