diff --git a/pandas/core/series.py b/pandas/core/series.py index 36ca2c0c6e097..c9b1a2c45eab3 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3288,23 +3288,27 @@ def transform(self, func, axis=0, *args, **kwargs): def apply(self, func, convert_dtype=True, args=(), **kwds): """ - Invoke function on values of Series. Can be ufunc (a NumPy function - that applies to the entire Series) or a Python function that only works - on single values + Invoke function on values of Series. + + Can be ufunc (a NumPy function that applies to the entire Series) or a + Python function that only works on single values. Parameters ---------- func : function - convert_dtype : boolean, default True + Python function or NumPy ufunc to apply. + convert_dtype : bool, default True Try to find better dtype for elementwise function results. If - False, leave as dtype=object + False, leave as dtype=object. args : tuple - Positional arguments to pass to function in addition to the value - Additional keyword arguments will be passed as keywords to the function + Positional arguments passed to func after the series value. + **kwds + Additional keyword arguments passed to func. Returns ------- - y : Series or DataFrame if func returns a Series + Series or DataFrame + If func returns a Series object the result will be a DataFrame. See Also -------- @@ -3314,12 +3318,11 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): Examples -------- - Create a series with typical summer temperatures for each city. - >>> series = pd.Series([20, 21, 12], index=['London', - ... 'New York','Helsinki']) - >>> series + >>> s = pd.Series([20, 21, 12], + ... index=['London', 'New York', 'Helsinki']) + >>> s London 20 New York 21 Helsinki 12 @@ -3329,8 +3332,8 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): argument to ``apply()``. >>> def square(x): - ... return x**2 - >>> series.apply(square) + ... return x ** 2 + >>> s.apply(square) London 400 New York 441 Helsinki 144 @@ -3339,7 +3342,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): Square the values by passing an anonymous function as an argument to ``apply()``. - >>> series.apply(lambda x: x**2) + >>> s.apply(lambda x: x ** 2) London 400 New York 441 Helsinki 144 @@ -3350,9 +3353,9 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): ``args`` keyword. >>> def subtract_custom_value(x, custom_value): - ... return x-custom_value + ... return x - custom_value - >>> series.apply(subtract_custom_value, args=(5,)) + >>> s.apply(subtract_custom_value, args=(5,)) London 15 New York 16 Helsinki 7 @@ -3363,10 +3366,10 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): >>> def add_custom_values(x, **kwargs): ... for month in kwargs: - ... x+=kwargs[month] + ... x += kwargs[month] ... return x - >>> series.apply(add_custom_values, june=30, july=20, august=25) + >>> s.apply(add_custom_values, june=30, july=20, august=25) London 95 New York 96 Helsinki 87 @@ -3374,7 +3377,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): Use a function from the Numpy library. - >>> series.apply(np.log) + >>> s.apply(np.log) London 2.995732 New York 3.044522 Helsinki 2.484907