From 52db3566fd960317e884fcaa495fc5b9722f6656 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 15:15:38 +0100 Subject: [PATCH 01/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 87 ++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..e97caa4bff753 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4818,66 +4818,82 @@ def aggregate(self, func, axis=0, *args, **kwargs): def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds): - """Applies function along an axis of the DataFrame. + """ + Apply a function along an axis of the `Series`. - Objects passed to functions are Series objects having index - either the DataFrame's index (axis=0) or the columns (axis=1). - Final return type depends on the return type of the applied function, - or on the `result_type` argument. + Objects passed to the function are `Series` objects having as index + either the DataFrame's index (`axis=0`) + or the DataFrame's columns (`axis=1`). + If `result_type` is None, the final return type is the return + type of the applied function. + Otherwise, it depends on the `result_type` argument. Parameters ---------- func : function - Function to apply to each column/row + Function to apply to each column or row. axis : {0 or 'index', 1 or 'columns'}, default 0 - * 0 or 'index': apply function to each column - * 1 or 'columns': apply function to each row + Axis along which the function is applied: + + * 0 or 'index': apply function to each column. + * 1 or 'columns': apply function to each row. broadcast : boolean, optional - For aggregation functions, return object of same size with values - propagated + Only relevant for aggregation functions: + + * `False` or `None` : returns a `Series` whose length is the length + of the index or the number of columns (based on the `axis` + parameter) + * `True` : results will be broadcast to the original shape + of the frame, the original index and columns will be retained. .. deprecated:: 0.23.0 This argument will be removed in a future version, replaced by result_type='broadcast'. raw : boolean, default False - If False, convert each row or column into a Series. If raw=True the - passed function will receive ndarray objects instead. If you are - just applying a NumPy reduction function this will achieve much - better performance + * `False` : passes each row or column into a `Series` to the + function. + * `True` : the passed function will receive ndarray objects + instead. + If you are just applying a NumPy reduction function this will + achieve much better performance. reduce : boolean or None, default None - Try to apply reduction procedures. If the DataFrame is empty, - apply will use reduce to determine whether the result should be a - Series or a DataFrame. If reduce is None (the default), apply's - return value will be guessed by calling func an empty Series (note: - while guessing, exceptions raised by func will be ignored). If - reduce is True a Series will always be returned, and if False a - DataFrame will always be returned. - - .. deprecated:: 0.23.0 + Try to apply reduction procedures. If the `DataFrame` is empty, + :meth:`apply` will use reduce to determine whether the result + should be a `Series` or a `DataFrame`. If reduce is None (the + default), :meth:`apply`'s return value will be guessed by calling + func on an empty `Series` + (note: while guessing, exceptions raised by `func` will be + ignored). + If reduce is True a `Series` will always be returned, and if + `False` a `DataFrame` will always be returned. + + .. deprecated:: 0.23.0. This argument will be removed in a future version, replaced by result_type='reduce'. - result_type : {'expand', 'reduce', 'broadcast, None} - These only act when axis=1 {columns}: + result_type : {'expand', 'reduce', 'broadcast', None} + These only act when `axis=1` (columns): * 'expand' : list-like results will be turned into columns. - * 'reduce' : return a Series if possible rather than expanding - list-like results. This is the opposite to 'expand'. + * 'reduce' : returns a `Series` if possible rather than expanding + list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape - of the frame, the original index & columns will be retained. + of the `DataFrame`, the original index and columns will be + retained. - The default behaviour (None) depends on the return value of the - applied function: list-like results will be returned as a Series - of those. However if the apply function returns a Series these + The default behaviour (`None`) depends on the return value of the + applied function: list-like results will be returned as a `Series` + of those. However if the apply function returns a `Series` these are expanded to columns. - .. versionadded:: 0.23.0 + .. versionadded:: 0.23.0. args : tuple Positional arguments to pass to function in addition to the - array/series - Additional keyword arguments will be passed as keywords to the function + array/series. + kwds : + Additional keyword arguments to pass as keywords to the function. Notes ----- @@ -4941,6 +4957,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 3 [1, 2] 4 [1, 2] 5 [1, 2] + dtype: object Passing result_type='expand' will expand list-like results to columns of a Dataframe @@ -4958,7 +4975,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, ``result_type='expand'``. The resulting column names will be the Series index. - >>> df.apply(lambda x: Series([1, 2], index=['foo', 'bar']), axis=1) + >>> df.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1) foo bar 0 1 2 1 1 2 From c593a70c51ba193e9618131eb668737096cd2efb Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 22:54:03 +0100 Subject: [PATCH 02/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e97caa4bff753..92866ca3bbbea 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4819,9 +4819,9 @@ def aggregate(self, func, axis=0, *args, **kwargs): def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds): """ - Apply a function along an axis of the `Series`. + Apply a function along an axis of the DataFrame. - Objects passed to the function are `Series` objects having as index + Objects passed to the function are Series objects having as index either the DataFrame's index (`axis=0`) or the DataFrame's columns (`axis=1`). If `result_type` is None, the final return type is the return @@ -4840,7 +4840,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, broadcast : boolean, optional Only relevant for aggregation functions: - * `False` or `None` : returns a `Series` whose length is the length + * `False` or `None` : returns a Series whose length is the length of the index or the number of columns (based on the `axis` parameter) * `True` : results will be broadcast to the original shape @@ -4851,7 +4851,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, by result_type='broadcast'. raw : boolean, default False - * `False` : passes each row or column into a `Series` to the + * `False` : passes each row or column into a Series to the function. * `True` : the passed function will receive ndarray objects instead. @@ -4860,12 +4860,12 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, reduce : boolean or None, default None Try to apply reduction procedures. If the `DataFrame` is empty, :meth:`apply` will use reduce to determine whether the result - should be a `Series` or a `DataFrame`. If reduce is None (the + should be a Series or a `DataFrame`. If reduce is None (the default), :meth:`apply`'s return value will be guessed by calling - func on an empty `Series` + func on an empty Series (note: while guessing, exceptions raised by `func` will be ignored). - If reduce is True a `Series` will always be returned, and if + If reduce is True a Series will always be returned, and if `False` a `DataFrame` will always be returned. .. deprecated:: 0.23.0. @@ -4876,15 +4876,15 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, These only act when `axis=1` (columns): * 'expand' : list-like results will be turned into columns. - * 'reduce' : returns a `Series` if possible rather than expanding + * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape of the `DataFrame`, the original index and columns will be retained. The default behaviour (`None`) depends on the return value of the - applied function: list-like results will be returned as a `Series` - of those. However if the apply function returns a `Series` these + applied function: list-like results will be returned as a Series + of those. However if the apply function returns a Series these are expanded to columns. .. versionadded:: 0.23.0. From 8caf5f615a17ccaf738d15553a5d8938dcf0cfde Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:13:26 +0100 Subject: [PATCH 03/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 92866ca3bbbea..41228d2688cf4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4851,7 +4851,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, by result_type='broadcast'. raw : boolean, default False - * `False` : passes each row or column into a Series to the + * `False` : passes each row or column as a Series to the function. * `True` : the passed function will receive ndarray objects instead. From c1214b42cf686fc61609894476572e0a82b14142 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:16:00 +0100 Subject: [PATCH 04/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 41228d2688cf4..74b697ec75843 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4822,8 +4822,8 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Apply a function along an axis of the DataFrame. Objects passed to the function are Series objects having as index - either the DataFrame's index (`axis=0`) - or the DataFrame's columns (`axis=1`). + either the DataFrame's index (``axis=0``) + or the DataFrame's columns (``axis=1``). If `result_type` is None, the final return type is the return type of the applied function. Otherwise, it depends on the `result_type` argument. @@ -4873,7 +4873,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, by result_type='reduce'. result_type : {'expand', 'reduce', 'broadcast', None} - These only act when `axis=1` (columns): + These only act when ``axis=1`` (columns): * 'expand' : list-like results will be turned into columns. * 'reduce' : returns a Series if possible rather than expanding From 2432ff573d7b59b4cd1db02cbf8945603f514145 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:18:27 +0100 Subject: [PATCH 05/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 74b697ec75843..a7ab6034447eb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4892,7 +4892,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, args : tuple Positional arguments to pass to function in addition to the array/series. - kwds : + **kwds Additional keyword arguments to pass as keywords to the function. Notes From da64c864c57edc2737e27cc79849e0c872dbdaf4 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:25:27 +0100 Subject: [PATCH 06/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a7ab6034447eb..97ba368c2c6ba 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4824,7 +4824,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Objects passed to the function are Series objects having as index either the DataFrame's index (``axis=0``) or the DataFrame's columns (``axis=1``). - If `result_type` is None, the final return type is the return + If ``result_type is None``, the final return type is the return type of the applied function. Otherwise, it depends on the `result_type` argument. @@ -4857,10 +4857,10 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, instead. If you are just applying a NumPy reduction function this will achieve much better performance. - reduce : boolean or None, default None + reduce : boolean or `None`, default `None` Try to apply reduction procedures. If the `DataFrame` is empty, :meth:`apply` will use reduce to determine whether the result - should be a Series or a `DataFrame`. If reduce is None (the + should be a Series or a `DataFrame`. If ``reduce is None`` (the default), :meth:`apply`'s return value will be guessed by calling func on an empty Series (note: while guessing, exceptions raised by `func` will be @@ -4872,7 +4872,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, This argument will be removed in a future version, replaced by result_type='reduce'. - result_type : {'expand', 'reduce', 'broadcast', None} + result_type : {'expand', 'reduce', 'broadcast', `None`} These only act when ``axis=1`` (columns): * 'expand' : list-like results will be turned into columns. From dbd90c1b82a151004ae650406f58645389b34f12 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:27:52 +0100 Subject: [PATCH 07/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 97ba368c2c6ba..f862c0c60ca80 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4837,7 +4837,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, * 0 or 'index': apply function to each column. * 1 or 'columns': apply function to each row. - broadcast : boolean, optional + broadcast : bool, optional Only relevant for aggregation functions: * `False` or `None` : returns a Series whose length is the length @@ -4850,14 +4850,14 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, This argument will be removed in a future version, replaced by result_type='broadcast'. - raw : boolean, default False + raw : bool, default False * `False` : passes each row or column as a Series to the function. * `True` : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance. - reduce : boolean or `None`, default `None` + reduce : bool or `None`, default `None` Try to apply reduction procedures. If the `DataFrame` is empty, :meth:`apply` will use reduce to determine whether the result should be a Series or a `DataFrame`. If ``reduce is None`` (the From 6cbadd8eda46b6f373ae8bb3451868f93e9a2c04 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:32:30 +0100 Subject: [PATCH 08/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f862c0c60ca80..60f387099b2fe 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4890,10 +4890,11 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, .. versionadded:: 0.23.0. args : tuple - Positional arguments to pass to function in addition to the + Positional arguments to pass to `func` in addition to the array/series. **kwds - Additional keyword arguments to pass as keywords to the function. + Additional keyword arguments to pass as keywords arguments to + `func`. Notes ----- From eaf144156aff5e4246ae558d0089d5ed4b5b881f Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sat, 10 Mar 2018 23:36:17 +0100 Subject: [PATCH 09/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 60f387099b2fe..c857d4f3829c6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4821,7 +4821,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, """ Apply a function along an axis of the DataFrame. - Objects passed to the function are Series objects having as index + Objects passed to the function are Series objects whose index is either the DataFrame's index (``axis=0``) or the DataFrame's columns (``axis=1``). If ``result_type is None``, the final return type is the return From 3794d19892a396e8df670c1f864020a6270cadde Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sun, 11 Mar 2018 00:05:52 +0100 Subject: [PATCH 10/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c857d4f3829c6..e5e7a8e8f6a2e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4855,8 +4855,8 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, function. * `True` : the passed function will receive ndarray objects instead. - If you are just applying a NumPy reduction function this will - achieve much better performance. + If you are just applying a NumPy reduction function this will + achieve much better performance. reduce : bool or `None`, default `None` Try to apply reduction procedures. If the `DataFrame` is empty, :meth:`apply` will use reduce to determine whether the result From 0105edbcb9fbfe52b7761bafa225a4612bed649b Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sun, 11 Mar 2018 00:19:58 +0100 Subject: [PATCH 11/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e5e7a8e8f6a2e..65e76a4fd5a21 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4859,18 +4859,18 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, achieve much better performance. reduce : bool or `None`, default `None` Try to apply reduction procedures. If the `DataFrame` is empty, - :meth:`apply` will use reduce to determine whether the result + :meth:`apply` will use `reduce` to determine whether the result should be a Series or a `DataFrame`. If ``reduce is None`` (the default), :meth:`apply`'s return value will be guessed by calling - func on an empty Series + `func` on an empty Series (note: while guessing, exceptions raised by `func` will be ignored). - If reduce is True a Series will always be returned, and if - `False` a `DataFrame` will always be returned. + If ``reduce is True`` a Series will always be returned, and if + ``reduce is False`` a `DataFrame` will always be returned. .. deprecated:: 0.23.0. This argument will be removed in a future version, replaced - by result_type='reduce'. + by ``result_type='reduce'``. result_type : {'expand', 'reduce', 'broadcast', `None`} These only act when ``axis=1`` (columns): @@ -4898,9 +4898,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Notes ----- - In the current implementation apply calls func twice on the + In the current implementation apply calls `func` twice on the first column/row to decide whether it can take a fast or slow - code path. This can lead to unexpected behavior if func has + code path. This can lead to unexpected behavior if `func` has side-effects, as they will take effect twice for the first column/row. From 4d3e8cf35908be6e7104fb37e305f28c791936a8 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Sun, 11 Mar 2018 00:20:38 +0100 Subject: [PATCH 12/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 65e76a4fd5a21..959ea792fc4e8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4858,15 +4858,15 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, If you are just applying a NumPy reduction function this will achieve much better performance. reduce : bool or `None`, default `None` - Try to apply reduction procedures. If the `DataFrame` is empty, + Try to apply reduction procedures. If the DataFrame is empty, :meth:`apply` will use `reduce` to determine whether the result - should be a Series or a `DataFrame`. If ``reduce is None`` (the + should be a Series or a DataFrame. If ``reduce is None`` (the default), :meth:`apply`'s return value will be guessed by calling `func` on an empty Series (note: while guessing, exceptions raised by `func` will be ignored). If ``reduce is True`` a Series will always be returned, and if - ``reduce is False`` a `DataFrame` will always be returned. + ``reduce is False`` a DataFrame will always be returned. .. deprecated:: 0.23.0. This argument will be removed in a future version, replaced @@ -4879,7 +4879,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape - of the `DataFrame`, the original index and columns will be + of the DataFrame, the original index and columns will be retained. The default behaviour (`None`) depends on the return value of the From c4da1ea7231acd2d0862367a15588e8493fe2d89 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 10:22:44 +0100 Subject: [PATCH 13/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 959ea792fc4e8..208a48f0a7987 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4882,7 +4882,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, of the DataFrame, the original index and columns will be retained. - The default behaviour (`None`) depends on the return value of the + The default behaviour `None` depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these are expanded to columns. From ee6919efd2a10e2fb6b331c8feb5c1391db571ed Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 13:16:04 +0100 Subject: [PATCH 14/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 208a48f0a7987..d7c383b65ce6f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4859,9 +4859,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, achieve much better performance. reduce : bool or `None`, default `None` Try to apply reduction procedures. If the DataFrame is empty, - :meth:`apply` will use `reduce` to determine whether the result + `apply` will use `reduce` to determine whether the result should be a Series or a DataFrame. If ``reduce is None`` (the - default), :meth:`apply`'s return value will be guessed by calling + default), `apply`'s return value will be guessed by calling `func` on an empty Series (note: while guessing, exceptions raised by `func` will be ignored). From 23be01b34e95aa952421317cd6dad1e525615cdd Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 13:16:58 +0100 Subject: [PATCH 15/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d7c383b65ce6f..3d0144087309a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4822,11 +4822,10 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is - either the DataFrame's index (``axis=0``) - or the DataFrame's columns (``axis=1``). - If ``result_type is None``, the final return type is the return - type of the applied function. - Otherwise, it depends on the `result_type` argument. + either the DataFrame's index (``axis=0``) or the DataFrame's columns + (``axis=1``). If ``result_type is None``, the final return type is the + return type of the applied function. Otherwise, it depends on the + `result_type` argument. Parameters ---------- From 51ad0cb6c3cfddc68719d07a07c3a2801492e35b Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 13:22:44 +0100 Subject: [PATCH 16/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3d0144087309a..56bd2491c86eb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4823,9 +4823,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Objects passed to the function are Series objects whose index is either the DataFrame's index (``axis=0``) or the DataFrame's columns - (``axis=1``). If ``result_type is None``, the final return type is the - return type of the applied function. Otherwise, it depends on the - `result_type` argument. + (``axis=1``). If ``result_type is None``, the final return type is + inferred from the return type of the applied function. Otherwise, it + depends on the `result_type` argument. Parameters ---------- From cf2f2d06b847adb7d67692cf80c4e12aa865ef4b Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 14:39:47 +0100 Subject: [PATCH 17/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 56bd2491c86eb..568f30f54375b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4867,7 +4867,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, If ``reduce is True`` a Series will always be returned, and if ``reduce is False`` a DataFrame will always be returned. - .. deprecated:: 0.23.0. + .. deprecated:: 0.23.0 This argument will be removed in a future version, replaced by ``result_type='reduce'``. @@ -4886,7 +4886,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, of those. However if the apply function returns a Series these are expanded to columns. - .. versionadded:: 0.23.0. + .. versionadded:: 0.23.0 args : tuple Positional arguments to pass to `func` in addition to the From d52ac81e7e3cc2503516df473c78de6161cec086 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 15:10:41 +0100 Subject: [PATCH 18/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 69 ++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 568f30f54375b..8aa54c28169ab 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4906,46 +4906,33 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Examples -------- - We use this DataFrame to illustrate - - >>> df = pd.DataFrame(np.tile(np.arange(3), 6).reshape(6, -1) + 1, - ... columns=['A', 'B', 'C']) + >>> df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B']) >>> df - A B C - 0 1 2 3 - 1 1 2 3 - 2 1 2 3 - 3 1 2 3 - 4 1 2 3 - 5 1 2 3 + A B + 0 4 9 + 1 4 9 + 2 4 9 Using a numpy universal function (in this case the same as ``np.sqrt(df)``): >>> df.apply(np.sqrt) - A B C - 0 1.0 1.414214 1.732051 - 1 1.0 1.414214 1.732051 - 2 1.0 1.414214 1.732051 - 3 1.0 1.414214 1.732051 - 4 1.0 1.414214 1.732051 - 5 1.0 1.414214 1.732051 + A B + 0 2.0 3.0 + 1 2.0 3.0 + 2 2.0 3.0 Using a reducing function on either axis >>> df.apply(np.sum, axis=0) - A 6 - B 12 - C 18 + A 12 + B 27 dtype: int64 >>> df.apply(np.sum, axis=1) - 0 6 - 1 6 - 2 6 - 3 6 - 4 6 - 5 6 + 0 13 + 1 13 + 2 13 dtype: int64 Retuning a list-like will result in a Series @@ -4954,9 +4941,12 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 0 [1, 2] 1 [1, 2] 2 [1, 2] - 3 [1, 2] - 4 [1, 2] - 5 [1, 2] + dtype: object + + >>> df.apply(lambda x: [1,], axis=1) + 0 [1] + 1 [1] + 2 [1] dtype: object Passing result_type='expand' will expand list-like results @@ -4967,9 +4957,6 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 0 1 2 1 1 2 2 1 2 - 3 1 2 - 4 1 2 - 5 1 2 Returning a Series inside the function is similar to passing ``result_type='expand'``. The resulting column names @@ -4980,23 +4967,17 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 0 1 2 1 1 2 2 1 2 - 3 1 2 - 4 1 2 - 5 1 2 Passing ``result_type='broadcast'`` will ensure the same shape result, whether list-like or scalar is returned by the function, and broadcast it along the axis. The resulting column names will be the originals. - >>> df.apply(lambda x: [1, 2, 3], axis=1, result_type='broadcast') - A B C - 0 1 2 3 - 1 1 2 3 - 2 1 2 3 - 3 1 2 3 - 4 1 2 3 - 5 1 2 3 + >>> df.apply(lambda x: [1, 2], axis=1, result_type='broadcast') + A B + 0 1 2 + 1 1 2 + 2 1 2 See also -------- From 3160fb805ee8b4784b08ba979a3132f21e072ed8 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 15:15:25 +0100 Subject: [PATCH 19/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8aa54c28169ab..fb64855a3dd1f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4943,12 +4943,6 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 2 [1, 2] dtype: object - >>> df.apply(lambda x: [1,], axis=1) - 0 [1] - 1 [1] - 2 [1] - dtype: object - Passing result_type='expand' will expand list-like results to columns of a Dataframe From dfa0f5571852a36e8b4727b687c9e1aece301ab4 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 16:56:04 +0100 Subject: [PATCH 20/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fb64855a3dd1f..4779ff991334e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4859,13 +4859,13 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, reduce : bool or `None`, default `None` Try to apply reduction procedures. If the DataFrame is empty, `apply` will use `reduce` to determine whether the result - should be a Series or a DataFrame. If ``reduce is None`` (the + should be a Series or a DataFrame. If ``reduce=None`` (the default), `apply`'s return value will be guessed by calling `func` on an empty Series (note: while guessing, exceptions raised by `func` will be ignored). - If ``reduce is True`` a Series will always be returned, and if - ``reduce is False`` a DataFrame will always be returned. + If ``reduce=True`` a Series will always be returned, and if + ``reduce=False`` a DataFrame will always be returned. .. deprecated:: 0.23.0 This argument will be removed in a future version, replaced From 037c7298afa1975a95c4616275fb53abd466f761 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 17:04:11 +0100 Subject: [PATCH 21/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4779ff991334e..9e1a88bce492a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4856,7 +4856,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, instead. If you are just applying a NumPy reduction function this will achieve much better performance. - reduce : bool or `None`, default `None` + reduce : bool or None, default None Try to apply reduction procedures. If the DataFrame is empty, `apply` will use `reduce` to determine whether the result should be a Series or a DataFrame. If ``reduce=None`` (the From d757960db539ea68c486b91425468e96da626164 Mon Sep 17 00:00:00 2001 From: mdeboc Date: Mon, 12 Mar 2018 17:13:48 +0100 Subject: [PATCH 22/24] DOC: update the pandas.DataFrame.apply docstring --- pandas/core/frame.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9e1a88bce492a..0428903956bc7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4903,6 +4903,12 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, side-effects, as they will take effect twice for the first column/row. + See also + -------- + DataFrame.applymap: For elementwise operations + DataFrame.aggregate: only perform aggregating type operations + DataFrame.transform: only perform transformating type operations + Examples -------- @@ -4973,12 +4979,6 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, 1 1 2 2 1 2 - See also - -------- - DataFrame.applymap: For elementwise operations - DataFrame.aggregate: only perform aggregating type operations - DataFrame.transform: only perform transformating type operations - Returns ------- applied : Series or DataFrame From 17039dcaff41bd2e5f4bb5a8e2ea97a1f846bd3d Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 12 Mar 2018 17:45:23 +0100 Subject: [PATCH 23/24] Update frame.py --- pandas/core/frame.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0428903956bc7..df8be0005be96 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4823,9 +4823,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, Objects passed to the function are Series objects whose index is either the DataFrame's index (``axis=0``) or the DataFrame's columns - (``axis=1``). If ``result_type is None``, the final return type is - inferred from the return type of the applied function. Otherwise, it - depends on the `result_type` argument. + (``axis=1``). By default (``result_type=None``), the final return type + is inferred from the return type of the applied function. Otherwise, + it depends on the `result_type` argument. Parameters ---------- @@ -4839,10 +4839,10 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, broadcast : bool, optional Only relevant for aggregation functions: - * `False` or `None` : returns a Series whose length is the length + * ``False`` or ``None`` : returns a Series whose length is the length of the index or the number of columns (based on the `axis` parameter) - * `True` : results will be broadcast to the original shape + * ``True`` : results will be broadcast to the original shape of the frame, the original index and columns will be retained. .. deprecated:: 0.23.0 @@ -4850,9 +4850,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, by result_type='broadcast'. raw : bool, default False - * `False` : passes each row or column as a Series to the + * ``False`` : passes each row or column as a Series to the function. - * `True` : the passed function will receive ndarray objects + * ``True`` : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance. @@ -4871,7 +4871,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, This argument will be removed in a future version, replaced by ``result_type='reduce'``. - result_type : {'expand', 'reduce', 'broadcast', `None`} + result_type : {'expand', 'reduce', 'broadcast', None}, default None These only act when ``axis=1`` (columns): * 'expand' : list-like results will be turned into columns. @@ -4881,7 +4881,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, of the DataFrame, the original index and columns will be retained. - The default behaviour `None` depends on the return value of the + The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these are expanded to columns. From c68a0129b2b9fc9dd093f3cc15adc08a946a017d Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 12 Mar 2018 17:46:55 +0100 Subject: [PATCH 24/24] pep8 --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index df8be0005be96..249703bedcc21 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4839,9 +4839,9 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, broadcast : bool, optional Only relevant for aggregation functions: - * ``False`` or ``None`` : returns a Series whose length is the length - of the index or the number of columns (based on the `axis` - parameter) + * ``False`` or ``None`` : returns a Series whose length is the + length of the index or the number of columns (based on the + `axis` parameter) * ``True`` : results will be broadcast to the original shape of the frame, the original index and columns will be retained.