From b75fddc9388e1cc050cd7acb183aca8adabf4585 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Thu, 27 Feb 2020 14:48:12 +0700 Subject: [PATCH 01/10] DOC: Fix errors in pandas.Series.argmin --- pandas/core/base.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index b9aeb32eea5c1..5340d7199a498 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1019,21 +1019,50 @@ def min(self, axis=None, skipna=True, *args, **kwargs): def argmin(self, axis=None, skipna=True, *args, **kwargs): """ - Return a ndarray of the minimum argument indexer. + Return int position of the smallest value in the Series. + + If the minimum is achieved in multiple locations, + the first row position is returned. Parameters ---------- axis : {None} Dummy argument for consistency with Series. skipna : bool, default True + Exclude NA/null values when showing the result. + *args, **kwargs + Additional arguments and keywords for compatibility with NumPy. Returns ------- - numpy.ndarray + int + Row position of the minimum values. See Also -------- - numpy.ndarray.argmin + numpy.ndarray.argmin : Equivalent method for numpy arrays. + Series.argmax : Similar method, but returning the maximum. + Series.idxmin : Return index label of the minimum values. + Series.idxmax : Return index label of the maximum values. + + Examples + -------- + Consider dataset containing cereal calories + + >>> s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0, + ... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0}) + >>> s + Corn Flakes 100.0 + Almond Delight 110.0 + Cinnamon Toast Crunch 120.0 + Cocoa Puff 110.0 + dtype: float64 + + >>> s.argmin() + 0 + + The minimum cereal calories is in the first element, + since series is zero-indexed. """ nv.validate_minmax_axis(axis) nv.validate_argmax_with_skipna(skipna, args, kwargs) From 353ce65633e6049e3814d764bd93f145000e3373 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Mon, 2 Mar 2020 14:22:11 +0700 Subject: [PATCH 02/10] refactor argmax and argmin doc using @doc --- pandas/core/base.py | 87 ++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 57 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 716d5bbd2526e..e356aba82314c 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -927,16 +927,25 @@ def max(self, axis=None, skipna=True, *args, **kwargs): nv.validate_max(args, kwargs) return nanops.nanmax(self._values, skipna=skipna) + @doc( + operation='maximum', + oppose_operation='minimum', + value='largest', + position='third', + klass='argmax', + oppose_klass='argmin', + example_values=2 + ) def argmax(self, axis=None, skipna=True, *args, **kwargs): """ - Return int position of the largest value in the Series. + Return int position of the {value} value in the Series. - If the maximum is achieved in multiple locations, + If the {operation} is achieved in multiple locations, the first row position is returned. Parameters ---------- - axis : {None} + axis : {{None}} Dummy argument for consistency with Series. skipna : bool, default True Exclude NA/null values when showing the result. @@ -946,12 +955,12 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): Returns ------- int - Row position of the maximum values. + Row position of the {operation} values. See Also -------- - numpy.ndarray.argmax : Equivalent method for numpy arrays. - Series.argmin : Similar method, but returning the minimum. + numpy.ndarray.{klass} : Equivalent method for numpy arrays. + Series.{oppose_klass} : Similar method, but returning the {oppose_operation}. Series.idxmax : Return index label of the maximum values. Series.idxmin : Return index label of the minimum values. @@ -959,8 +968,8 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): -------- Consider dataset containing cereal calories - >>> s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0, - ... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0}) + >>> s = pd.Series({{'Corn Flakes': 100.0, 'Almond Delight': 110.0, + ... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0}}) >>> s Corn Flakes 100.0 Almond Delight 110.0 @@ -968,10 +977,10 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): Cocoa Puff 110.0 dtype: float64 - >>> s.argmax() - 2 + >>> s.{klass}() + {example_values} - The maximum cereal calories is in the third element, + The {operation} cereal calories is in the {position} element, since series is zero-indexed. """ nv.validate_minmax_axis(axis) @@ -1019,53 +1028,17 @@ def min(self, axis=None, skipna=True, *args, **kwargs): nv.validate_min(args, kwargs) return nanops.nanmin(self._values, skipna=skipna) + @doc( + argmax, + operation='minimum', + oppose_operation='maximum', + value='smallest', + position='first', + klass='argmin', + oppose_klass='argmax', + example_values=0 + ) def argmin(self, axis=None, skipna=True, *args, **kwargs): - """ - Return int position of the smallest value in the Series. - - If the minimum is achieved in multiple locations, - the first row position is returned. - - Parameters - ---------- - axis : {None} - Dummy argument for consistency with Series. - skipna : bool, default True - Exclude NA/null values when showing the result. - *args, **kwargs - Additional arguments and keywords for compatibility with NumPy. - - Returns - ------- - int - Row position of the minimum values. - - See Also - -------- - numpy.ndarray.argmin : Equivalent method for numpy arrays. - Series.argmax : Similar method, but returning the maximum. - Series.idxmin : Return index label of the minimum values. - Series.idxmax : Return index label of the maximum values. - - Examples - -------- - Consider dataset containing cereal calories - - >>> s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0, - ... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0}) - >>> s - Corn Flakes 100.0 - Almond Delight 110.0 - Cinnamon Toast Crunch 120.0 - Cocoa Puff 110.0 - dtype: float64 - - >>> s.argmin() - 0 - - The minimum cereal calories is in the first element, - since series is zero-indexed. - """ nv.validate_minmax_axis(axis) nv.validate_argmax_with_skipna(skipna, args, kwargs) return nanops.nanargmin(self._values, skipna=skipna) From 3acc9dd5abb206bd0f9510dc5f4cc2e62445059b Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Mon, 2 Mar 2020 20:29:27 +0700 Subject: [PATCH 03/10] formatting using black pandas --- pandas/core/base.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index e356aba82314c..1f3c8bd0b5057 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -928,13 +928,13 @@ def max(self, axis=None, skipna=True, *args, **kwargs): return nanops.nanmax(self._values, skipna=skipna) @doc( - operation='maximum', - oppose_operation='minimum', - value='largest', - position='third', - klass='argmax', - oppose_klass='argmin', - example_values=2 + operation="maximum", + oppose_operation="minimum", + value="largest", + position="third", + klass="argmax", + oppose_klass="argmin", + example_values="2", ) def argmax(self, axis=None, skipna=True, *args, **kwargs): """ @@ -1030,13 +1030,13 @@ def min(self, axis=None, skipna=True, *args, **kwargs): @doc( argmax, - operation='minimum', - oppose_operation='maximum', - value='smallest', - position='first', - klass='argmin', - oppose_klass='argmax', - example_values=0 + operation="minimum", + oppose_operation="maximum", + value="smallest", + position="first", + klass="argmin", + oppose_klass="argmax", + example_values="0", ) def argmin(self, axis=None, skipna=True, *args, **kwargs): nv.validate_minmax_axis(axis) From 2bdd0881066d64b3c7267432e4e32fa8bf3445f2 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Tue, 3 Mar 2020 12:58:29 +0700 Subject: [PATCH 04/10] reduce parameter count from args --- pandas/core/base.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 1f3c8bd0b5057..93acb7de5606b 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -928,19 +928,17 @@ def max(self, axis=None, skipna=True, *args, **kwargs): return nanops.nanmax(self._values, skipna=skipna) @doc( - operation="maximum", - oppose_operation="minimum", + op="max", + oppose="min", value="largest", position="third", - klass="argmax", - oppose_klass="argmin", example_values="2", ) def argmax(self, axis=None, skipna=True, *args, **kwargs): """ Return int position of the {value} value in the Series. - If the {operation} is achieved in multiple locations, + If the {op}imum is achieved in multiple locations, the first row position is returned. Parameters @@ -955,12 +953,12 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): Returns ------- int - Row position of the {operation} values. + Row position of the {op}imum value. See Also -------- - numpy.ndarray.{klass} : Equivalent method for numpy arrays. - Series.{oppose_klass} : Similar method, but returning the {oppose_operation}. + numpy.ndarray.arg{op} : Equivalent method for numpy arrays. + Series.arg{oppose} : Similar method, but returning the {oppose}inimum. Series.idxmax : Return index label of the maximum values. Series.idxmin : Return index label of the minimum values. @@ -977,10 +975,10 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): Cocoa Puff 110.0 dtype: float64 - >>> s.{klass}() + >>> s.arg{op}() {example_values} - The {operation} cereal calories is in the {position} element, + The {op}imum cereal calories is the {position} element, since series is zero-indexed. """ nv.validate_minmax_axis(axis) From 4de38a7eac9ee7e7fc29dcfd8ee9b9775fce0c5f Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Tue, 3 Mar 2020 13:00:51 +0700 Subject: [PATCH 05/10] reduce parameter count from args --- pandas/core/base.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 93acb7de5606b..f0fab7af20179 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1028,12 +1028,10 @@ def min(self, axis=None, skipna=True, *args, **kwargs): @doc( argmax, - operation="minimum", - oppose_operation="maximum", + op="min", + oppose="max", value="smallest", position="first", - klass="argmin", - oppose_klass="argmax", example_values="0", ) def argmin(self, axis=None, skipna=True, *args, **kwargs): From 1714e128f7508d23786e94ccbf8d7c4dcc147fad Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Date: Tue, 3 Mar 2020 22:39:40 +0700 Subject: [PATCH 06/10] Update pandas/core/base.py Co-Authored-By: Simon Hawkins --- pandas/core/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index f0fab7af20179..4d96e3e4647ab 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -958,7 +958,7 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): See Also -------- numpy.ndarray.arg{op} : Equivalent method for numpy arrays. - Series.arg{oppose} : Similar method, but returning the {oppose}inimum. + Series.arg{oppose} : Similar method, but returning the {oppose}imum. Series.idxmax : Return index label of the maximum values. Series.idxmin : Return index label of the minimum values. From 7f055ba192d2aafdb5fc5750c4e1acd824b0c3a5 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Wed, 4 Mar 2020 07:48:59 +0700 Subject: [PATCH 07/10] formatting by black pandas --- pandas/core/base.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index f867009e42cf0..12a4f2d772261 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -928,11 +928,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs): return nanops.nanmax(self._values, skipna=skipna) @doc( - op="max", - oppose="min", - value="largest", - position="third", - example_values="2", + op="max", oppose="min", value="largest", position="third", example_values="2", ) def argmax(self, axis=None, skipna=True, *args, **kwargs): """ From c89126a8e5c9bfcc302d3fd1f3e7a4a8cfd06f6f Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Wed, 4 Mar 2020 14:50:04 +0700 Subject: [PATCH 08/10] combine examples and self-referencing --- pandas/core/base.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 12a4f2d772261..aba942244fa98 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -928,7 +928,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs): return nanops.nanmax(self._values, skipna=skipna) @doc( - op="max", oppose="min", value="largest", position="third", example_values="2", + op="max", oppose="min", value="largest", ) def argmax(self, axis=None, skipna=True, *args, **kwargs): """ @@ -953,6 +953,7 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): See Also -------- + Series.arg{op} : Return position of the {op}imum value. numpy.ndarray.arg{op} : Equivalent method for numpy arrays. Series.arg{oppose} : Similar method, but returning the {oppose}imum. Series.idxmax : Return index label of the maximum values. @@ -971,10 +972,13 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): Cocoa Puff 110.0 dtype: float64 - >>> s.arg{op}() - {example_values} + >>> s.argmax() + 2 + >>> s.argmin() + 0 - The {op}imum cereal calories is the {position} element, + The maximum cereal calories is the third element and + the minimum cereal calories is the first element, since series is zero-indexed. """ nv.validate_minmax_axis(axis) @@ -1023,12 +1027,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs): return nanops.nanmin(self._values, skipna=skipna) @doc( - argmax, - op="min", - oppose="max", - value="smallest", - position="first", - example_values="0", + argmax, op="min", oppose="max", value="smallest", ) def argmin(self, axis=None, skipna=True, *args, **kwargs): nv.validate_minmax_axis(axis) From 04c197ab04a1d8326ff98d05735d210ff20302a7 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Wed, 4 Mar 2020 15:24:03 +0700 Subject: [PATCH 09/10] remove argmax and argmin variables in See Also --- pandas/core/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index aba942244fa98..60aaafc62d702 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -953,9 +953,9 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): See Also -------- - Series.arg{op} : Return position of the {op}imum value. + Series.argmax : Return position of the maximum value. + Series.argmin : Return position of the minimum value. numpy.ndarray.arg{op} : Equivalent method for numpy arrays. - Series.arg{oppose} : Similar method, but returning the {oppose}imum. Series.idxmax : Return index label of the maximum values. Series.idxmin : Return index label of the minimum values. From 183833a8dc669c8c810f9d824b023d247fc547c1 Mon Sep 17 00:00:00 2001 From: Farhan Reynaldo Hutabarat Date: Thu, 5 Mar 2020 14:09:00 +0700 Subject: [PATCH 10/10] make doc decorator one-liner --- pandas/core/base.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 60aaafc62d702..508582540e169 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -927,9 +927,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs): nv.validate_max(args, kwargs) return nanops.nanmax(self._values, skipna=skipna) - @doc( - op="max", oppose="min", value="largest", - ) + @doc(op="max", oppose="min", value="largest") def argmax(self, axis=None, skipna=True, *args, **kwargs): """ Return int position of the {value} value in the Series. @@ -953,8 +951,8 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs): See Also -------- - Series.argmax : Return position of the maximum value. - Series.argmin : Return position of the minimum value. + Series.arg{op} : Return position of the {op}imum value. + Series.arg{oppose} : Return position of the {oppose}imum value. numpy.ndarray.arg{op} : Equivalent method for numpy arrays. Series.idxmax : Return index label of the maximum values. Series.idxmin : Return index label of the minimum values. @@ -1026,9 +1024,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs): nv.validate_min(args, kwargs) return nanops.nanmin(self._values, skipna=skipna) - @doc( - argmax, op="min", oppose="max", value="smallest", - ) + @doc(argmax, op="min", oppose="max", value="smallest") def argmin(self, axis=None, skipna=True, *args, **kwargs): nv.validate_minmax_axis(axis) nv.validate_argmax_with_skipna(skipna, args, kwargs)