Skip to content
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

CLN: NDFrame._where #38742

Merged
merged 4 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ Sparse

ExtensionArray
^^^^^^^^^^^^^^

- Bug in :meth:`DataFrame.where` when ``other`` is a :class:`Series` with ExtensionArray dtype (:issue:`38729`)
-
-

Expand Down
39 changes: 30 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@
import pandas as pd
from pandas.core import arraylike, indexing, missing, nanops
import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
from pandas.core.base import PandasObject, SelectionMixin
import pandas.core.common as com
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.construction import create_series_with_explicit_dtype, extract_array
from pandas.core.flags import Flags
from pandas.core.indexes import base as ibase
from pandas.core.indexes.api import (
Expand Down Expand Up @@ -8786,6 +8787,9 @@ def _where(
"""
inplace = validate_bool_kwarg(inplace, "inplace")

if axis is not None:
jreback marked this conversation as resolved.
Show resolved Hide resolved
axis = self._get_axis_number(axis)

# align the cond to same shape as myself
cond = com.apply_if_callable(cond, self)
if isinstance(cond, NDFrame):
Expand Down Expand Up @@ -8825,22 +8829,39 @@ def _where(
if other.ndim <= self.ndim:

_, other = self.align(
other, join="left", axis=axis, level=level, fill_value=np.nan
other,
join="left",
axis=axis,
level=level,
fill_value=np.nan,
copy=False,
)

# if we are NOT aligned, raise as we cannot where index
if axis is None and not all(
other._get_axis(i).equals(ax) for i, ax in enumerate(self.axes)
):
if axis is None and not other._indexed_same(self):
raise InvalidIndexError

elif other.ndim < self.ndim:
# TODO(EA2D): avoid object-dtype cast in EA case GH#38729
other = other._values
if axis == 0:
other = np.reshape(other, (-1, 1))
elif axis == 1:
other = np.reshape(other, (1, -1))

other = np.broadcast_to(other, self.shape)

# slice me out of the other
else:
raise NotImplementedError(
"cannot align with a higher dimensional NDFrame"
)

if isinstance(other, np.ndarray):
if not isinstance(other, (MultiIndex, NDFrame)):
# mainly just catching Index here
other = extract_array(other, extract_numpy=True)

if isinstance(other, (np.ndarray, ExtensionArray)):

if other.shape != self.shape:

Expand Down Expand Up @@ -8885,10 +8906,10 @@ def _where(
else:
align = self._get_axis_number(axis) == 1

if align and isinstance(other, NDFrame):
other = other.reindex(self._info_axis, axis=self._info_axis_number)
if isinstance(cond, NDFrame):
cond = cond.reindex(self._info_axis, axis=self._info_axis_number)
cond = cond.reindex(
self._info_axis, axis=self._info_axis_number, copy=False
)

block_axis = self._get_block_manager_axis(axis)

Expand Down
11 changes: 1 addition & 10 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,7 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
# If the default repeat behavior in np.putmask would go in the
# wrong direction, then explicitly repeat and reshape new instead
if getattr(new, "ndim", 0) >= 1:
if self.ndim - 1 == new.ndim and axis == 1:
new = np.repeat(new, new_values.shape[-1]).reshape(self.shape)
new = new.astype(new_values.dtype)
new = new.astype(new_values.dtype, copy=False)

# we require exact matches between the len of the
# values we are setting (or is compat). np.putmask
Expand Down Expand Up @@ -1104,13 +1102,6 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
new = new.T
axis = new_values.ndim - axis - 1

# Pseudo-broadcast
if getattr(new, "ndim", 0) >= 1:
if self.ndim - 1 == new.ndim:
new_shape = list(new.shape)
new_shape.insert(axis, 1)
new = new.reshape(tuple(new_shape))

# operate column-by-column
def f(mask, val, idx):

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,22 @@ def test_where_categorical_filtering(self):
expected.loc[0, :] = np.nan

tm.assert_equal(result, expected)

jreback marked this conversation as resolved.
Show resolved Hide resolved
def test_where_ea_other(self):
# GH#38729/GH#38742
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
arr = pd.array([7, pd.NA, 9])
ser = Series(arr)
mask = np.ones(df.shape, dtype=bool)
mask[1, :] = False

# TODO: ideally we would get Int64 instead of object
result = df.where(mask, ser, axis=0)
expected = DataFrame({"A": [1, pd.NA, 3], "B": [4, pd.NA, 6]}).astype(object)
tm.assert_frame_equal(result, expected)

ser2 = Series(arr[:2], index=["A", "B"])
expected = DataFrame({"A": [1, 7, 3], "B": [4, pd.NA, 6]})
expected["B"] = expected["B"].astype(object)
result = df.where(mask, ser2, axis=1)
tm.assert_frame_equal(result, expected)