diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 988d63db2229f..fa4f4b8cf4b45 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -283,6 +283,7 @@ 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`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 0f1cd397f2dd1..2a3119515bb99 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -933,7 +933,9 @@ 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): + # 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): continue