Skip to content

Commit fc813e7

Browse files
authored
BUG: Series multiplication with timedelta scalar numexpr path (pandas-dev#31529)
1 parent 08895fa commit fc813e7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v1.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Timezones
4444
Numeric
4545
^^^^^^^
4646
- Bug in dtypes being lost in ``DataFrame.__invert__`` (``~`` operator) with mixed dtypes (:issue:`31183`)
47+
- Bug in :class:`Series` multiplication when multiplying a numeric :class:`Series` with >10000 elements with a timedelta-like scalar (:issue:`31467`)
4748
-
4849

4950
Conversion

pandas/core/ops/array_ops.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010

11-
from pandas._libs import Timestamp, lib, ops as libops
11+
from pandas._libs import Timedelta, Timestamp, lib, ops as libops
1212

1313
from pandas.core.dtypes.cast import (
1414
construct_1d_object_array_from_listlike,
@@ -186,11 +186,12 @@ def arithmetic_op(
186186
rvalues = maybe_upcast_for_op(rvalues, lvalues.shape)
187187

188188
if should_extension_dispatch(left, rvalues) or isinstance(
189-
rvalues, (ABCTimedeltaArray, ABCDatetimeArray, Timestamp)
189+
rvalues, (ABCTimedeltaArray, ABCDatetimeArray, Timestamp, Timedelta)
190190
):
191191
# TimedeltaArray, DatetimeArray, and Timestamp are included here
192192
# because they have `freq` attribute which is handled correctly
193193
# by dispatch_to_extension_op.
194+
# Timedelta is included because numexpr will fail on it, see GH#31457
194195
res_values = dispatch_to_extension_op(op, lvalues, rvalues)
195196

196197
else:

pandas/tests/arithmetic/test_numeric.py

+22
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box):
177177
commute = scalar_td * index
178178
tm.assert_equal(commute, expected)
179179

180+
@pytest.mark.parametrize(
181+
"scalar_td",
182+
[
183+
Timedelta(days=1),
184+
Timedelta(days=1).to_timedelta64(),
185+
Timedelta(days=1).to_pytimedelta(),
186+
],
187+
ids=lambda x: type(x).__name__,
188+
)
189+
def test_numeric_arr_mul_tdscalar_numexpr_path(self, scalar_td, box):
190+
arr = np.arange(2 * 10 ** 4).astype(np.int64)
191+
obj = tm.box_expected(arr, box, transpose=False)
192+
193+
expected = arr.view("timedelta64[D]").astype("timedelta64[ns]")
194+
expected = tm.box_expected(expected, box, transpose=False)
195+
196+
result = obj * scalar_td
197+
tm.assert_equal(result, expected)
198+
199+
result = scalar_td * obj
200+
tm.assert_equal(result, expected)
201+
180202
def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box):
181203
index = numeric_idx[1:3]
182204

0 commit comments

Comments
 (0)