diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 63f5bd547074a..8c6a5c9d020b4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8652,12 +8652,7 @@ def _where( self._check_inplace_setting(other) new_data = self._data.putmask( - mask=cond, - new=other, - align=align, - inplace=True, - axis=block_axis, - transpose=self._AXIS_REVERSED, + mask=cond, new=other, align=align, axis=block_axis, ) self._update_inplace(new_data) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index c429a65ed3369..935ff09585b17 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -910,25 +910,26 @@ def setitem(self, indexer, value): def putmask( self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False, - ): + ) -> List["Block"]: """ putmask the data to the block; it is possible that we may create a new dtype of block - return the resulting block(s) + Return the resulting block(s). Parameters ---------- - mask : the condition to respect + mask : the condition to respect new : a ndarray/object - inplace : perform inplace modification, default is False + inplace : bool, default False + Perform inplace modification. axis : int - transpose : boolean - Set to True if self is stored with axes reversed + transpose : bool, default False + Set to True if self is stored with axes reversed. Returns ------- - a list of new blocks, the result of the putmask + List[Block] """ new_values = self.values if inplace else self.values.copy() @@ -1626,23 +1627,10 @@ def set(self, locs, values): self.values[:] = values def putmask( - self, mask, new, inplace=False, axis=0, transpose=False, - ): + self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False, + ) -> List["Block"]: """ - putmask the data to the block; we must be a single block and not - generate other blocks - - return the resulting block - - Parameters - ---------- - mask : the condition to respect - new : a ndarray/object - inplace : perform inplace modification, default is False - - Returns - ------- - a new block, the result of the putmask + See Block.putmask.__doc__ """ inplace = validate_bool_kwarg(inplace, "inplace") diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 330acec46f5cd..b245ac09029a2 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -558,14 +558,25 @@ def where(self, **kwargs) -> "BlockManager": def setitem(self, indexer, value) -> "BlockManager": return self.apply("setitem", indexer=indexer, value=value) - def putmask(self, **kwargs): + def putmask( + self, mask, new, align: bool = True, axis: int = 0, + ): + transpose = self.ndim == 2 - if kwargs.pop("align", True): + if align: align_keys = ["new", "mask"] else: align_keys = ["mask"] - return self.apply("putmask", align_keys=align_keys, **kwargs) + return self.apply( + "putmask", + align_keys=align_keys, + mask=mask, + new=new, + inplace=True, + axis=axis, + transpose=transpose, + ) def diff(self, n: int, axis: int) -> "BlockManager": return self.apply("diff", n=n, axis=axis) diff --git a/pandas/core/series.py b/pandas/core/series.py index 9c1f4134746a8..1e1c9963ab3f1 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2812,7 +2812,7 @@ def update(self, other) -> None: other = other.reindex_like(self) mask = notna(other) - self._data = self._data.putmask(mask=mask, new=other, inplace=True) + self._data = self._data.putmask(mask=mask, new=other) self._maybe_update_cacher() # ----------------------------------------------------------------------