Skip to content

Commit cae7ca5

Browse files
phoflmroeschke
authored and
Yi Wei
committed
Improve performance when selecting rows and columns (pandas-dev#53014)
* Improve performance when selecting rows and columns * Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * Update indexing.py * Update v2.1.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 98f130d commit cae7ca5

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ Performance improvements
285285
- Performance improvement accessing :attr:`arrays.IntegerArrays.dtype` & :attr:`arrays.FloatingArray.dtype` (:issue:`52998`)
286286
- Performance improvement in :class:`Series` reductions (:issue:`52341`)
287287
- Performance improvement in :func:`concat` when ``axis=1`` and objects have different indexes (:issue:`52541`)
288+
- Performance improvement in :meth:`DataFrame.loc` when selecting rows and columns (:issue:`53014`)
288289
- Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`)
289290
- Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`)
290291
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`)

pandas/core/indexing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,9 @@ def _getitem_tuple_same_dim(self, tup: tuple):
933933
This is only called after a failed call to _getitem_lowerdim.
934934
"""
935935
retval = self.obj
936-
for i, key in enumerate(tup):
936+
# Selecting columns before rows is signficiantly faster
937+
for i, key in enumerate(reversed(tup)):
938+
i = self.ndim - i - 1
937939
if com.is_null_slice(key):
938940
continue
939941

0 commit comments

Comments
 (0)