Skip to content

Commit a01fa79

Browse files
jbrockmendeltm9k1
authored andcommitted
BUG: Fix ndarray + DataFrame ops (pandas-dev#23114)
1 parent 5e96555 commit a01fa79

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ Numeric
845845
- Bug in :class:`DataFrame` multiplication between boolean dtype and integer returning ``object`` dtype instead of integer dtype (:issue:`22047`, :issue:`22163`)
846846
- Bug in :meth:`DataFrame.apply` where, when supplied with a string argument and additional positional or keyword arguments (e.g. ``df.apply('sum', min_count=1)``), a ``TypeError`` was wrongly raised (:issue:`22376`)
847847
- Bug in :meth:`DataFrame.astype` to extension dtype may raise ``AttributeError`` (:issue:`22578`)
848-
848+
- Bug in :class:`DataFrame` with ``timedelta64[ns]`` dtype arithmetic operations with ``ndarray`` with integer dtype incorrectly treating the narray as ``timedelta64[ns]`` dtype (:issue:`23114`)
849849

850850
Strings
851851
^^^^^^^

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,10 @@ def __round__(self, decimals=0):
17951795
# ----------------------------------------------------------------------
17961796
# Array Interface
17971797

1798+
# This is also set in IndexOpsMixin
1799+
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
1800+
__array_priority__ = 1000
1801+
17981802
def __array__(self, dtype=None):
17991803
return com.values_from_object(self)
18001804

pandas/tests/arithmetic/test_numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box):
156156
if box is not pd.Index and broken:
157157
# np.timedelta64(3, 'D') / 2 == np.timedelta64(1, 'D')
158158
raise pytest.xfail("timedelta64 not converted to nanos; "
159-
"Tick division not imlpemented")
159+
"Tick division not implemented")
160160

161161
expected = TimedeltaIndex(['3 Days', '36 Hours'])
162162

pandas/tests/arithmetic/test_object.py

-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ def test_objarr_radd_str_invalid(self, dtype, data, box):
140140
operator.sub, ops.rsub])
141141
def test_objarr_add_invalid(self, op, box):
142142
# invalid ops
143-
if box is pd.DataFrame and op is ops.radd:
144-
pytest.xfail(reason="DataFrame op incorrectly casts the np.array"
145-
"case to M8[ns]")
146143

147144
obj_ser = tm.makeObjectSeries()
148145
obj_ser.name = 'objects'

pandas/tests/arithmetic/test_timedelta64.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ def test_td64arr_add_str_invalid(self, box):
364364
operator.sub, ops.rsub],
365365
ids=lambda x: x.__name__)
366366
def test_td64arr_add_sub_float(self, box, op, other):
367+
if box is pd.DataFrame and isinstance(other, np.ndarray):
368+
pytest.xfail("Tries to broadcast, raising "
369+
"ValueError instead of TypeError")
370+
367371
tdi = TimedeltaIndex(['-1 days', '-1 days'])
368372
tdi = tm.box_expected(tdi, box)
369373

370-
if box is pd.DataFrame and op in [operator.add, operator.sub]:
371-
pytest.xfail(reason="Tries to align incorrectly, "
372-
"raises ValueError")
373-
374374
with pytest.raises(TypeError):
375375
op(tdi, other)
376376

@@ -1126,9 +1126,6 @@ def test_td64arr_floordiv_tdscalar(self, box, scalar_td):
11261126

11271127
def test_td64arr_rfloordiv_tdscalar(self, box, scalar_td):
11281128
# GH#18831
1129-
if box is pd.DataFrame and isinstance(scalar_td, np.timedelta64):
1130-
pytest.xfail(reason="raises TypeError, not sure why")
1131-
11321129
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
11331130
td1.iloc[2] = np.nan
11341131

pandas/tests/frame/test_analytics.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2046,8 +2046,11 @@ def test_matmul(self):
20462046

20472047
# np.array @ DataFrame
20482048
result = operator.matmul(a.values, b)
2049+
assert isinstance(result, DataFrame)
2050+
assert result.columns.equals(b.columns)
2051+
assert result.index.equals(pd.Index(range(3)))
20492052
expected = np.dot(a.values, b.values)
2050-
tm.assert_almost_equal(result, expected)
2053+
tm.assert_almost_equal(result.values, expected)
20512054

20522055
# nested list @ DataFrame (__rmatmul__)
20532056
result = operator.matmul(a.values.tolist(), b)

0 commit comments

Comments
 (0)