Skip to content

Commit 6e5e393

Browse files
authored
PERF: DataFrame.loc and Series.loc when key is a MultiIndex (#56062)
1 parent 8f0c4d2 commit 6e5e393

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

asv_bench/benchmarks/indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ def time_loc_null_slice_plus_slice(self, unique_levels):
306306
target = (self.tgt_null_slice, self.tgt_slice)
307307
self.df.loc[target, :]
308308

309+
def time_loc_multiindex(self, unique_levels):
310+
target = self.df.index[::10]
311+
self.df.loc[target]
312+
309313
def time_xs_level_0(self, unique_levels):
310314
target = self.tgt_scalar
311315
self.df.xs(target, level=0)

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Performance improvements
320320
- Performance improvement in :func:`read_stata` for files with many variables (:issue:`55515`)
321321
- Performance improvement in :func:`to_dict` on converting DataFrame to dictionary (:issue:`50990`)
322322
- Performance improvement in :meth:`DataFrame.groupby` when aggregating pyarrow timestamp and duration dtypes (:issue:`55031`)
323+
- Performance improvement in :meth:`DataFrame.loc` and :meth:`Series.loc` when indexing with a :class:`MultiIndex` (:issue:`56062`)
323324
- Performance improvement in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` when indexed by a :class:`MultiIndex` (:issue:`54835`)
324325
- Performance improvement in :meth:`Index.difference` (:issue:`55108`)
325326
- Performance improvement in :meth:`MultiIndex.get_indexer` when ``method`` is not ``None`` (:issue:`55839`)

pandas/core/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pandas.core.dtypes.generic import (
4343
ABCExtensionArray,
4444
ABCIndex,
45+
ABCMultiIndex,
4546
ABCSeries,
4647
)
4748
from pandas.core.dtypes.inference import iterable_not_string
@@ -121,7 +122,9 @@ def is_bool_indexer(key: Any) -> bool:
121122
check_array_indexer : Check that `key` is a valid array to index,
122123
and convert to an ndarray.
123124
"""
124-
if isinstance(key, (ABCSeries, np.ndarray, ABCIndex, ABCExtensionArray)):
125+
if isinstance(
126+
key, (ABCSeries, np.ndarray, ABCIndex, ABCExtensionArray)
127+
) and not isinstance(key, ABCMultiIndex):
125128
if key.dtype == np.object_:
126129
key_array = np.asarray(key)
127130

0 commit comments

Comments
 (0)