From 7f77219427103739dc4ae41d23a32dc79305c5a1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sun, 30 Apr 2023 16:08:52 +0200 Subject: [PATCH 1/4] Improve performance when selecting rows and columns --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/indexing.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 988d63db2229f..8f1ebf60eddbb 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -286,6 +286,7 @@ Performance improvements - Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`) - Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`) - Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`) +- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 0f1cd397f2dd1..8bfd32b21181b 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -933,7 +933,8 @@ def _getitem_tuple_same_dim(self, tup: tuple): This is only called after a failed call to _getitem_lowerdim. """ retval = self.obj - for i, key in enumerate(tup): + for i, key in enumerate(reversed(tup)): + i = self.ndim - i - 1 if com.is_null_slice(key): continue From 82a011d48033308249c8abff6e012312fd0e3bf7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 2 May 2023 23:10:02 +0200 Subject: [PATCH 2/4] Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 8f1ebf60eddbb..c0a262654aaa4 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -286,7 +286,7 @@ Performance improvements - Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`) - Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`) - Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`) -- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014) +- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014`) - .. --------------------------------------------------------------------------- From abe96ebd45535ce865806a0a2c90ef4d49b7a3f1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 2 May 2023 23:10:51 +0200 Subject: [PATCH 3/4] Update indexing.py --- pandas/core/indexing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 8bfd32b21181b..2a3119515bb99 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -933,6 +933,7 @@ def _getitem_tuple_same_dim(self, tup: tuple): This is only called after a failed call to _getitem_lowerdim. """ retval = self.obj + # Selecting columns before rows is signficiantly faster for i, key in enumerate(reversed(tup)): i = self.ndim - i - 1 if com.is_null_slice(key): From 01c9eab8352394c29c4790caa8415c7180a0ba3b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 3 May 2023 23:23:16 +0200 Subject: [PATCH 4/4] Update v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index c0a262654aaa4..fa4f4b8cf4b45 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -283,10 +283,10 @@ Performance improvements - Performance improvement accessing :attr:`arrays.IntegerArrays.dtype` & :attr:`arrays.FloatingArray.dtype` (:issue:`52998`) - Performance improvement in :class:`Series` reductions (:issue:`52341`) - Performance improvement in :func:`concat` when ``axis=1`` and objects have different indexes (:issue:`52541`) +- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014`) - Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`) - Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`) - Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`) -- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014`) - .. ---------------------------------------------------------------------------