Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
# mypy: ignore-errors
import pandas as pd
import numpy as np
import pandas._testing as tm
df = pd.DataFrame(np.arange(50).reshape(10, 5)).notna().values
# -> works
NP_array = pd.array([i for i in range(10)], dtype=tm.SIGNED_INT_NUMPY_DTYPES[0]).reshape(10,1) #dtype: NumpyExtensionArray
# -> doesnt work (NotImplemented)
EA_array = pd.array([i for i in range(10)], dtype=tm.SIGNED_INT_EA_DTYPES[0]).reshape(10,1) #dtype: IntExtensionArray
print(df * NP_array)
# NotImplementedError: can only perform ops with 1-d structures
print(df * EA_array)
Issue Description
I was working on creating test cases for ExtensionArrays following comments on PR #61828 when I realized that I could not use the '&' operation on EAs like I could with NP arrays.
After a bit of digging around, it appears they both call self._logical_method, but whereas NP returns NotImplemented and continues operation, EA raises an error.
If someone wants to take a look while I work on something else, they are more than welcome to, otherwise I can work out a fix when I come back to it.
I have found that to be the case for both '*' and '&' so it's probably something deeper there
Expected Behavior
- Operators should function the same for both Numpy arrays and other ExtensionArrays
Installed Versions
Comment From: tisjayy
I confirmed the behavior, binary ops like * and & fail on 2D ExtensionArrays (Int64) but work fine with equivalent NumPy arrays. I'll look into _arith_method and _logical_method handling for 2D EAs and see if broadcasting can be better supported.
Comment From: tisjayy
take
Comment From: tisjayy
I changed the logical method in boolean.py so ExtensionArrays handle these cases like NumPy arrays do by returning NotImplemented when they can’t perform the operation. So Python can try other ways instead of showing error. Lets see if it passes the code checks.