Skip to content

Commit 30ffb16

Browse files
committed
PERF: significantly improve performance of MultiIndex.shape
1 parent a4c19e7 commit 30ffb16

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ Performance improvements
981981
- For :meth:`to_datetime` changed default value of cache parameter to ``True`` (:issue:`26043`)
982982
- Improved performance of :class:`DatetimeIndex` and :class:`PeriodIndex` slicing given non-unique, monotonic data (:issue:`27136`).
983983
- Improved performance of :meth:`pd.read_json` for index-oriented data. (:issue:`26773`)
984+
- Improved performance of :meth:`MultiIndex.shape` (:issue:`27384`).
984985
985986
.. _whatsnew_0250.bug_fixes:
986987

pandas/core/indexes/base.py

+7
Original file line numberDiff line numberDiff line change
@@ -5639,6 +5639,13 @@ def _add_logical_methods_disabled(cls):
56395639
cls.all = make_invalid_op("all")
56405640
cls.any = make_invalid_op("any")
56415641

5642+
@property
5643+
def shape(self):
5644+
"""
5645+
Return a tuple of the shape of the underlying data.
5646+
"""
5647+
return (len(self),)
5648+
56425649

56435650
Index._add_numeric_methods_disabled()
56445651
Index._add_logical_methods()

pandas/core/indexes/interval.py

-5
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,6 @@ def size(self):
405405
# Avoid materializing ndarray[Interval]
406406
return self._data.size
407407

408-
@property
409-
def shape(self):
410-
# Avoid materializing ndarray[Interval]
411-
return self._data.shape
412-
413408
@property
414409
def itemsize(self):
415410
msg = (

0 commit comments

Comments
 (0)