From 7e0d457b0b75b024916211503a7f52d9ab1a1a40 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 15 Feb 2021 20:15:23 +0100 Subject: [PATCH 1/2] PERF: use arr.size instead of np.prod(arr.shape) in _can_use_numexpr --- pandas/core/computation/expressions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/computation/expressions.py b/pandas/core/computation/expressions.py index 087b7f39e3374..ca62e8a31be7a 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -76,7 +76,7 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check): if op_str is not None: # required min elements (otherwise we are adding overhead) - if np.prod(a.shape) > _MIN_ELEMENTS: + if a.size > _MIN_ELEMENTS: # check for dtype compatibility dtypes: Set[str] = set() for o in [a, b]: From db677dec83ef8423eeca804982696bc147eb3a23 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 16 Feb 2021 21:03:26 +0100 Subject: [PATCH 2/2] more occurences --- pandas/core/array_algos/transforms.py | 2 +- pandas/core/dtypes/cast.py | 2 +- pandas/core/dtypes/missing.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/array_algos/transforms.py b/pandas/core/array_algos/transforms.py index 371425f325d76..1dde9b221a90b 100644 --- a/pandas/core/array_algos/transforms.py +++ b/pandas/core/array_algos/transforms.py @@ -19,7 +19,7 @@ def shift(values: np.ndarray, periods: int, axis: int, fill_value) -> np.ndarray new_values = new_values.T axis = new_values.ndim - axis - 1 - if np.prod(new_values.shape): + if new_values.size: new_values = np.roll(new_values, ensure_platform_int(periods), axis=axis) axis_indexer = [slice(None)] * values.ndim diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index e27c519304e2e..2fc6aa9842b73 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1497,7 +1497,7 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]): value = iNaT # we have an array of datetime or timedeltas & nulls - elif np.prod(value.shape) or not is_dtype_equal(value.dtype, dtype): + elif value.size or not is_dtype_equal(value.dtype, dtype): _disallow_mismatched_datetimelike(value, dtype) try: diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index ef645313de614..657b10c46fed7 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -429,7 +429,7 @@ def array_equivalent( # NaNs can occur in float and complex arrays. if is_float_dtype(left.dtype) or is_complex_dtype(left.dtype): - if not (np.prod(left.shape) and np.prod(right.shape)): + if not (left.size and right.size): return True return ((left == right) | (isna(left) & isna(right))).all()