Skip to content

BUG: flex op with DataFrame, Series and ea vs ndarray #34277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ Numeric
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
- Bug in :meth:`DataFrame.corr` and :meth:`DataFrame.cov` raising when handling nullable integer columns with ``pandas.NA`` (:issue:`33803`)
- Bug in :class:`DataFrame` and :class:`Series` addition and subtraction between object-dtype objects and ``datetime64`` dtype objects (:issue:`33824`)
- Bug in :class:`DataFrame` flex arithmetic methods with ExtensionDtypes (:issue:`????`)

Conversion
^^^^^^^^^^
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,23 @@ def apply(self, func, **kwargs) -> List["Block"]:
apply the function to my values; return a block if we are not
one
"""
kwargs = self._ensure_same_shape_values(kwargs)

with np.errstate(all="ignore"):
result = func(self.values, **kwargs)

return self._split_op_result(result)

def _ensure_same_shape_values(self, kwargs):
# TODO(EA2D): kludge for arithmetic not needed with 2D EA
if self.ndim == 2 and self.values.ndim == 1:
if "right" in kwargs and isinstance(kwargs["right"], np.ndarray):
right = kwargs["right"]
if right.ndim == 2:
assert right.shape == (1, len(self.values)), right.shape
kwargs["right"] = right[0]
return kwargs

def _split_op_result(self, result) -> List["Block"]:
# See also: split_and_operate
if is_extension_array_dtype(result) and result.ndim > 1:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname):
result = getattr(empty, opname)(const).dtypes.value_counts()
tm.assert_series_equal(result, pd.Series([2], index=[np.dtype(bool)]))

def test_df_flex_cmp_ea_dtype_with_ndarray_series(self):
ii = pd.IntervalIndex.from_breaks([1, 2, 3])
df = pd.DataFrame({"A": ii, "B": ii})

ser = pd.Series([0, 0])
res = df.eq(ser, axis=0)

expected = pd.DataFrame({"A": [False, False], "B": [False, False]})
tm.assert_frame_equal(res, expected)

ser2 = pd.Series([1, 2], index=["A", "B"])
res2 = df.eq(ser2, axis=1)
tm.assert_frame_equal(res2, expected)


# -------------------------------------------------------------------
# Arithmetic
Expand Down