Skip to content

Commit aefe1a3

Browse files
authored
34297 sub slow in first call (#34354)
1 parent 760ba37 commit aefe1a3

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

asv_bench/benchmarks/arithmetic.py

+25
Original file line numberDiff line numberDiff line change
@@ -469,4 +469,29 @@ def time_apply_index(self, offset):
469469
offset.apply_index(self.rng)
470470

471471

472+
class BinaryOpsMultiIndex:
473+
params = ["sub", "add", "mul", "div"]
474+
param_names = ["func"]
475+
476+
def setup(self, func):
477+
date_range = pd.date_range("20200101 00:00", "20200102 0:00", freq="S")
478+
level_0_names = [str(i) for i in range(30)]
479+
480+
index = pd.MultiIndex.from_product([level_0_names, date_range])
481+
column_names = ["col_1", "col_2"]
482+
483+
self.df = pd.DataFrame(
484+
np.random.rand(len(index), 2), index=index, columns=column_names
485+
)
486+
487+
self.arg_df = pd.DataFrame(
488+
np.random.randint(1, 10, (len(level_0_names), 2)),
489+
index=level_0_names,
490+
columns=column_names,
491+
)
492+
493+
def time_binary_op_multiindex(self, func):
494+
getattr(self.df, func)(self.arg_df, level=0)
495+
496+
472497
from .pandas_vb_common import setup # noqa: F401 isort:skip

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ Performance improvements
642642
- Performance improvement in reductions (sum, prod, min, max) for nullable (integer and boolean) dtypes (:issue:`30982`, :issue:`33261`, :issue:`33442`).
643643
- Performance improvement in arithmetic operations between two :class:`DataFrame` objects (:issue:`32779`)
644644
- Performance improvement in :class:`pandas.core.groupby.RollingGroupby` (:issue:`34052`)
645+
- Performance improvement in arithmetic operations (sub, add, mul, div) for MultiIndex (:issue:`34297`)
645646

646647
.. ---------------------------------------------------------------------------
647648

pandas/core/indexes/multi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3217,7 +3217,8 @@ def equals(self, other) -> bool:
32173217
if not is_object_dtype(other.dtype):
32183218
# other cannot contain tuples, so cannot match self
32193219
return False
3220-
3220+
elif len(self) != len(other):
3221+
return False
32213222
return array_equivalent(self._values, other._values)
32223223

32233224
if self.nlevels != other.nlevels:

0 commit comments

Comments
 (0)