diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6a80fa3e93362..857b97f58d8a0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8935,9 +8935,7 @@ def _where( # reconstruct the block manager self._check_inplace_setting(other) - new_data = self._mgr.putmask( - mask=cond, new=other, align=align, axis=block_axis - ) + new_data = self._mgr.putmask(mask=cond, new=other, align=align) result = self._constructor(new_data) return self._update_inplace(result) diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 99edec3c606d4..90591370c3583 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -343,7 +343,7 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> ArrayManage # def setitem(self, indexer, value) -> ArrayManager: # return self.apply_with_block("setitem", indexer=indexer, value=value) - def putmask(self, mask, new, align: bool = True, axis: int = 0): + def putmask(self, mask, new, align: bool = True): if align: align_keys = ["new", "mask"] @@ -356,7 +356,6 @@ def putmask(self, mask, new, align: bool = True, axis: int = 0): align_keys=align_keys, mask=mask, new=new, - axis=axis, ) def diff(self, n: int, axis: int) -> ArrayManager: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 1356b9d3b2ca3..02c3340412983 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -998,7 +998,7 @@ def setitem(self, indexer, value): block = self.make_block(values) return block - def putmask(self, mask, new, axis: int = 0) -> List["Block"]: + def putmask(self, mask, new) -> List["Block"]: """ putmask the data to the block; it is possible that we may create a new dtype of block @@ -1009,7 +1009,6 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]: ---------- mask : np.ndarray[bool], SparseArray[bool], or BooleanArray new : a ndarray/object - axis : int Returns ------- @@ -1026,8 +1025,6 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]: new = self.fill_value if self._can_hold_element(new): - # We only get here for non-Extension Blocks, so _try_coerce_args - # is only relevant for DatetimeBlock and TimedeltaBlock if self.dtype.kind in ["m", "M"]: arr = self.array_values() arr = cast("NDArrayBackedExtensionArray", arr) @@ -1040,14 +1037,17 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]: new_values = new_values.T putmask_without_repeat(new_values, mask, new) + return [self] + + elif not mask.any(): + return [self] - # maybe upcast me - elif mask.any(): + else: + # may need to upcast if transpose: mask = mask.T if isinstance(new, np.ndarray): new = new.T - axis = new_values.ndim - axis - 1 # operate column-by-column def f(mask, val, idx): @@ -1074,8 +1074,6 @@ def f(mask, val, idx): new_blocks = self.split_and_operate(mask, f, True) return new_blocks - return [self] - def coerce_to_target_dtype(self, other): """ coerce the current block to a dtype compat for other @@ -1577,7 +1575,7 @@ def set_inplace(self, locs, values): assert locs.tolist() == [0] self.values = values - def putmask(self, mask, new, axis: int = 0) -> List["Block"]: + def putmask(self, mask, new) -> List["Block"]: """ See Block.putmask.__doc__ """ diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index ad9cdcfa1b07f..8e605a906eced 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -564,7 +564,7 @@ def where(self, other, cond, align: bool, errors: str, axis: int) -> BlockManage def setitem(self, indexer, value) -> BlockManager: return self.apply("setitem", indexer=indexer, value=value) - def putmask(self, mask, new, align: bool = True, axis: int = 0): + def putmask(self, mask, new, align: bool = True): if align: align_keys = ["new", "mask"] @@ -577,7 +577,6 @@ def putmask(self, mask, new, align: bool = True, axis: int = 0): align_keys=align_keys, mask=mask, new=new, - axis=axis, ) def diff(self, n: int, axis: int) -> BlockManager: