Skip to content

Commit e4cb663

Browse files
jbrockmendelfeefladder
authored andcommitted
REF: share Index.putmask with ExtensionIndex (pandas-dev#43221)
1 parent 34ccf05 commit e4cb663

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

pandas/core/indexes/base.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -4857,6 +4857,7 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
48574857
result = concat_compat(to_concat_vals)
48584858
return Index._with_infer(result, name=name)
48594859

4860+
@final
48604861
def putmask(self, mask, value) -> Index:
48614862
"""
48624863
Return a new Index of the values set with the mask.
@@ -4879,19 +4880,24 @@ def putmask(self, mask, value) -> Index:
48794880
try:
48804881
converted = self._validate_fill_value(value)
48814882
except (ValueError, TypeError) as err:
4882-
if is_object_dtype(self):
4883+
if is_object_dtype(self): # pragma: no cover
48834884
raise err
48844885

48854886
dtype = self._find_common_type_compat(value)
48864887
return self.astype(dtype).putmask(mask, value)
48874888

48884889
values = self._values.copy()
4889-
# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
4890-
# "Union[ExtensionArray, ndarray]"; expected "ndarray"
4891-
converted = setitem_datetimelike_compat(
4892-
values, mask.sum(), converted # type: ignore[arg-type]
4893-
)
4894-
np.putmask(values, mask, converted)
4890+
4891+
if isinstance(values, np.ndarray):
4892+
converted = setitem_datetimelike_compat(values, mask.sum(), converted)
4893+
np.putmask(values, mask, converted)
4894+
4895+
else:
4896+
# Note: we use the original value here, not converted, as
4897+
# _validate_fill_value is not idempotent
4898+
# error: "ExtensionArray" has no attribute "putmask"
4899+
values.putmask(mask, value) # type: ignore[attr-defined]
4900+
48954901
return self._shallow_copy(values)
48964902

48974903
def equals(self, other: Any) -> bool:

pandas/core/indexes/extension.py

-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
ABCSeries,
3232
)
3333

34-
from pandas.core.array_algos.putmask import validate_putmask
3534
from pandas.core.arrays import (
3635
Categorical,
3736
DatetimeArray,
@@ -323,21 +322,6 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
323322
# overriding IndexOpsMixin improves performance GH#38083
324323
return self._data.searchsorted(value, side=side, sorter=sorter)
325324

326-
def putmask(self, mask, value) -> Index:
327-
mask, noop = validate_putmask(self._data, mask)
328-
if noop:
329-
return self.copy()
330-
331-
try:
332-
self._validate_fill_value(value)
333-
except (ValueError, TypeError):
334-
dtype = self._find_common_type_compat(value)
335-
return self.astype(dtype).putmask(mask, value)
336-
337-
arr = self._data.copy()
338-
arr.putmask(mask, value)
339-
return type(self)._simple_new(arr, name=self.name)
340-
341325
# ---------------------------------------------------------------------
342326

343327
def _get_engine_target(self) -> np.ndarray:

0 commit comments

Comments
 (0)