Skip to content

ENH: implement EA._where #44187

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 3 commits into from
Oct 28, 2021
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 pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def putmask(self, mask: np.ndarray, value) -> None:

np.putmask(self._ndarray, mask, value)

def where(
def _where(
self: NDArrayBackedExtensionArrayT, mask: np.ndarray, value
) -> NDArrayBackedExtensionArrayT:
"""
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,31 @@ def insert(self: ExtensionArrayT, loc: int, item) -> ExtensionArrayT:

return type(self)._concat_same_type([self[:loc], item_arr, self[loc:]])

def _where(
self: ExtensionArrayT, mask: npt.NDArray[np.bool_], value
) -> ExtensionArrayT:
"""
Analogue to np.where(mask, self, value)

Parameters
----------
mask : np.ndarray[bool]
value : scalar or listlike

Returns
-------
same type as self
"""
result = self.copy()

if is_list_like(value):
val = value[~mask]
else:
val = value

result[~mask] = val
return result

@classmethod
def _empty(cls, shape: Shape, dtype: ExtensionDtype):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,13 @@ def to_dense(self) -> np.ndarray:

_internal_get_values = to_dense

def _where(self, mask, value):
# NB: may not preserve dtype, e.g. result may be Sparse[float64]
# while self is Sparse[int64]
naive_implementation = np.where(mask, self, value)
result = type(self)._from_sequence(naive_implementation)
return result

# ------------------------------------------------------------------------
# IO
# ------------------------------------------------------------------------
Expand Down
33 changes: 4 additions & 29 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
is_extension_array_dtype,
is_interval_dtype,
is_list_like,
is_sparse,
is_string_dtype,
)
from pandas.core.dtypes.dtypes import (
Expand Down Expand Up @@ -1626,30 +1625,9 @@ def where(self, other, cond, errors="raise") -> list[Block]:
# for the type
other = self.dtype.na_value

if is_sparse(self.values):
# TODO(SparseArray.__setitem__): remove this if condition
# We need to re-infer the type of the data after doing the
# where, for cases where the subtypes don't match
dtype = None
else:
dtype = self.dtype

result = self.values.copy()
icond = ~cond
if lib.is_scalar(other):
set_other = other
else:
set_other = other[icond]
try:
result[icond] = set_other
except (NotImplementedError, TypeError):
# NotImplementedError for class not implementing `__setitem__`
# TypeError for SparseArray, which implements just to raise
# a TypeError
if isinstance(result, Categorical):
# TODO: don't special-case
raise

result = self.values._where(cond, other)
except TypeError:
if is_interval_dtype(self.dtype):
# TestSetitemFloatIntervalWithIntIntervalValues
blk = self.coerce_to_target_dtype(other)
Expand All @@ -1658,10 +1636,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
# Interval[int64]->Interval[float64]
raise
return blk.where(other, cond, errors)

result = type(self.values)._from_sequence(
np.where(cond, self.values, other), dtype=dtype
)
raise

return [self.make_block_same_class(result)]

Expand Down Expand Up @@ -1751,7 +1726,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
cond = extract_bool_array(cond)

try:
res_values = arr.T.where(cond, other).T
res_values = arr.T._where(cond, other).T
except (ValueError, TypeError):
return Block.where(self, other, cond, errors=errors)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def test_where_non_categories(self):
msg = "Cannot setitem on a Categorical with a new category"
with pytest.raises(TypeError, match=msg):
# Test the Categorical method directly
ci._data.where(mask, 2)
ci._data._where(mask, 2)


class TestContains:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def test_where_datetimelike_categorical(tz_naive_fixture):
tm.assert_index_equal(res, dr)

# DatetimeArray.where
res = lvals._data.where(mask, rvals)
res = lvals._data._where(mask, rvals)
tm.assert_datetime_array_equal(res, dr._data)

# Series.where
Expand Down