From e0950695dde4674ed6d39aa72ef3239c3091d4ca Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 15 Jan 2019 15:59:58 -0800 Subject: [PATCH] PERF avoid object-dtype comparisons --- pandas/core/internals/blocks.py | 6 ++++++ pandas/util/testing.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 20881972c068a..df764aa4ba666 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2465,6 +2465,12 @@ def setitem(self, indexer, value): klass=ObjectBlock,) return newb.setitem(indexer, value) + def equals(self, other): + # override for significant performance improvement + if self.dtype != other.dtype or self.shape != other.shape: + return False + return (self.values.view('i8') == other.values.view('i8')).all() + class TimeDeltaBlock(DatetimeLikeBlockMixin, IntBlock): __slots__ = () diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 2df43cd678764..f441dd20f3982 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1199,6 +1199,11 @@ def assert_extension_array_equal(left, right, check_dtype=True, if check_dtype: assert_attr_equal('dtype', left, right, obj='ExtensionArray') + if hasattr(left, "asi8") and type(right) == type(left): + # Avoid slow object-dtype comparisons + assert_numpy_array_equal(left.asi8, right.asi8) + return + left_na = np.asarray(left.isna()) right_na = np.asarray(right.isna()) assert_numpy_array_equal(left_na, right_na, obj='ExtensionArray NA mask')