From cb97291ff05b37fcf6bc5495646ba4d72d1d8005 Mon Sep 17 00:00:00 2001 From: Seth Weiss Date: Thu, 8 Jun 2023 17:17:51 -0700 Subject: [PATCH 1/4] Add multi-conditional examples in loc docstring --- pandas/core/indexing.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 38bf6c34bf9c9..64dccbe4f0fd0 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -400,6 +400,31 @@ def loc(self) -> _LocIndexer: max_speed sidewinder 7 + Multiple conditional using ``and`` that returns a boolean Series + + >>> df.loc[(df['max_speed'] > 1) & (df['shield'] < 8)] + max_speed shield + viper 4 5 + + Multiple conditional using ``or`` that returns a boolean Series + + >>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)] + max_speed shield + cobra 1 2 + sidewinder 7 8 + + Please see the :ref:`user guide` + for more details and explanations of Boolean indexing. + + .. note:: + If you find yourself using 3 or more conditionals in ``.loc[]``, you may + experience performance gains by restructuring the DataFrame into a + MultiIndex object. + + See below for using ``.loc[]`` on MultiIndex DataFrames. + Please see the :ref:`user guide` + for more details and explanations of advanced indexing. + Callable that returns a boolean Series >>> df.loc[lambda df: df['shield'] == 8] From 7e5d7925c6823c1e15ed31bb4046d760cda0bd97 Mon Sep 17 00:00:00 2001 From: Seth Weiss Date: Fri, 9 Jun 2023 10:57:18 -0700 Subject: [PATCH 2/4] Update boolean operators in example descriptions --- pandas/core/indexing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 64dccbe4f0fd0..f6207732ab8d7 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -400,13 +400,13 @@ def loc(self) -> _LocIndexer: max_speed sidewinder 7 - Multiple conditional using ``and`` that returns a boolean Series + Multiple conditional using ``&`` that returns a boolean Series >>> df.loc[(df['max_speed'] > 1) & (df['shield'] < 8)] max_speed shield viper 4 5 - Multiple conditional using ``or`` that returns a boolean Series + Multiple conditional using ``|`` that returns a boolean Series >>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)] max_speed shield From d40f902064b77ac12824dd39448d88fecc438bf6 Mon Sep 17 00:00:00 2001 From: Seth Weiss Date: Fri, 9 Jun 2023 11:09:40 -0700 Subject: [PATCH 3/4] Update note on advanced indexing --- pandas/core/indexing.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index f6207732ab8d7..175a20c48ec82 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -417,13 +417,10 @@ def loc(self) -> _LocIndexer: for more details and explanations of Boolean indexing. .. note:: - If you find yourself using 3 or more conditionals in ``.loc[]``, you may - experience performance gains by restructuring the DataFrame into a - MultiIndex object. + If you find yourself using 3 or more conditionals in ``.loc[]``, + consider using :ref:`advanced indexing`. See below for using ``.loc[]`` on MultiIndex DataFrames. - Please see the :ref:`user guide` - for more details and explanations of advanced indexing. Callable that returns a boolean Series From 7f1105452593eb644d0389571c46bbf6bbaf1594 Mon Sep 17 00:00:00 2001 From: Seth Weiss Date: Sun, 11 Jun 2023 11:45:04 -0700 Subject: [PATCH 4/4] Add point to wrap conditionals in parens. --- pandas/core/indexing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 175a20c48ec82..4a2803f638c73 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -413,7 +413,8 @@ def loc(self) -> _LocIndexer: cobra 1 2 sidewinder 7 8 - Please see the :ref:`user guide` + Please ensure that each condition is wrapped in parentheses ``()``. + See the :ref:`user guide` for more details and explanations of Boolean indexing. .. note::