Skip to content

Commit 2fbdd1e

Browse files
authored
PERF: MultiIndex.size (#48723)
* add MultiIndex.size * whatsnew
1 parent 5e7cc40 commit 2fbdd1e

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

doc/source/whatsnew/v1.6.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Performance improvements
133133
~~~~~~~~~~~~~~~~~~~~~~~~
134134
- Performance improvement in :meth:`.DataFrameGroupBy.median` and :meth:`.SeriesGroupBy.median` and :meth:`.GroupBy.cumprod` for nullable dtypes (:issue:`37493`)
135135
- Performance improvement in :meth:`MultiIndex.argsort` and :meth:`MultiIndex.sort_values` (:issue:`48406`)
136+
- Performance improvement in :meth:`MultiIndex.size` (:issue:`48723`)
136137
- Performance improvement in :meth:`MultiIndex.union` without missing values and without duplicates (:issue:`48505`)
137138
- Performance improvement in :meth:`.DataFrameGroupBy.mean`, :meth:`.SeriesGroupBy.mean`, :meth:`.DataFrameGroupBy.var`, and :meth:`.SeriesGroupBy.var` for extension array dtypes (:issue:`37493`)
138139
- Performance improvement in :meth:`MultiIndex.isin` when ``level=None`` (:issue:`48622`)

pandas/core/indexes/multi.py

+8
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,14 @@ def dtypes(self) -> Series:
786786
def __len__(self) -> int:
787787
return len(self.codes[0])
788788

789+
@property
790+
def size(self) -> int:
791+
"""
792+
Return the number of elements in the underlying data.
793+
"""
794+
# override Index.size to avoid materializing _values
795+
return len(self)
796+
789797
# --------------------------------------------------------------------
790798
# Levels Methods
791799

0 commit comments

Comments
 (0)