BUG: EA inplace add (and other ops) with non-EA arg broken · Issue #37910 · pandas-dev/pandas (original) (raw)


Code Sample, a copy-pastable example

import numpy as np import pandas as pd

ser1 = pd.Series([1], dtype="Int64") ser2 = pd.Series([1.0], dtype=np.float64) ser1 += ser2

Note that the issue is NOT specific to the integer EA, but happens with all EAs that don't use NumPy dtypes.

Problem description

Traceback (most recent call last):
  File "bug.py", line 6, in <module>
    ser1 += ser2
  File ".../pandas/core/generic.py", line 11305, in __iadd__
    return self._inplace_method(other, type(self).__add__)  # type: ignore[operator]
  File ".../pandas/core/generic.py", line 11289, in _inplace_method
    if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
TypeError: Cannot interpret 'Int64Dtype()' as a data type

This is likely a fallout of #37508. See here:

def _inplace_method(self, other, op):
"""
Wrap arithmetic method to operate inplace.
"""
result = op(self, other)
if self.ndim == 1 and result._indexed_same(self) and result.dtype == self.dtype:
# GH#36498 this inplace op can _actually_ be inplace.
self._values[:] = result._values
return self

There, the result dtype (which is a NumPy dtype in this case) is compared with the EA dtype. This raises the TypeError, because NumPy only allows comparing its dtype objects with "real" NumPy dtypes (and EAs don't provide NumPy-compatible dtypes).

Expected Output

Pass. I'm pretty sure that worked with 1.0.3 (the mentioned PR in question was backported to the 1.0.x branch between the 1.0.3 and 1.0.4 release).