Skip to content
Prev Previous commit
Next Next commit
share EA putmask version between array and block manager
  • Loading branch information
jorisvandenbossche committed Nov 13, 2021
commit cf3486080730a20c02128e627fd97bfcc7f09c63
26 changes: 2 additions & 24 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
is_empty_indexer,
is_scalar_indexer,
)
from pandas.core.internals.methods import putmask_flexible_ea
import pandas.core.missing as missing

if TYPE_CHECKING:
Expand Down Expand Up @@ -1411,30 +1412,7 @@ def putmask(self, mask, new) -> list[Block]:
"""
See Block.putmask.__doc__
"""
mask = extract_bool_array(mask)

new_values = self.values

if mask.ndim == new_values.ndim + 1:
# TODO(EA2D): unnecessary with 2D EAs
mask = mask.reshape(new_values.shape)

try:
# Caller is responsible for ensuring matching lengths
new_values._putmask(mask, new)
except TypeError:
if not is_interval_dtype(self.dtype):
# Discussion about what we want to support in the general
# case GH#39584
raise

blk = self.coerce_to_target_dtype(new)
if blk.dtype == _dtype_obj:
# For now at least, only support casting e.g.
# Interval[int64]->Interval[float64],
raise
return blk.putmask(mask, new)

new_values = putmask_flexible_ea(self.values, mask, new)
nb = type(self)(new_values, placement=self._mgr_locs, ndim=self.ndim)
return [nb]

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/internals/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def putmask_flexible_ea(array: ExtensionArray, mask, new):
"""
mask = extract_bool_array(mask)

if mask.ndim == array.ndim + 1:
# TODO(EA2D): unnecessary with 2D EAs
mask = mask.reshape(array.shape)

if isinstance(array, NDArrayBackedExtensionArray):
if not can_hold_element(array, new):
array = _coerce_to_target_dtype(array, new)
Expand All @@ -109,6 +113,6 @@ def putmask_flexible_ea(array: ExtensionArray, mask, new):
# For now at least, only support casting e.g.
# Interval[int64]->Interval[float64],
raise
return putmask_flexible(array, mask, new)
return putmask_flexible_ea(array, mask, new)

return array