From 66a00fe6beef705c5c73df1dc8b3e5f3b4f9590c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 16 Mar 2020 19:52:36 -0700 Subject: [PATCH 1/4] TYP: annotate Block/BlockManager.putmask --- pandas/core/generic.py | 7 +----- pandas/core/internals/blocks.py | 38 +++++++++++++++++++------------ pandas/core/internals/managers.py | 16 +++++++++++-- pandas/core/series.py | 2 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8d56311331d4d..994c07169ba05 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8667,12 +8667,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 1a92a9486e9e4..b1144500c34e4 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -928,26 +928,28 @@ def putmask( 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 blocks. Parameters ---------- - mask : the condition to respect + mask : the condition to respect new : a ndarray/object - align : boolean, perform alignment on other/cond, default is True - inplace : perform inplace modification, default is False + align : bool, default True + Perform alignment on other/cond. + 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() @@ -1657,8 +1659,14 @@ def set(self, locs, values, check=False): self.values = values def putmask( - self, mask, new, align=True, inplace=False, axis=0, transpose=False, - ): + self, + mask, + new, + align: bool = True, + 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 @@ -1667,14 +1675,16 @@ def putmask( Parameters ---------- - mask : the condition to respect + mask : the condition to respect new : a ndarray/object - align : boolean, perform alignment on other/cond, default is True - inplace : perform inplace modification, default is False + align : bool, default True. + Perform alignment on other/cond. + inplace : bool, default False. + Perform inplace modification. Returns ------- - a new block, the result of the putmask + List[Block] """ inplace = validate_bool_kwarg(inplace, "inplace") diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 93d4b02310d54..61cae0c14e1d2 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -566,8 +566,20 @@ def where(self, **kwargs) -> "BlockManager": def setitem(self, indexer, value) -> "BlockManager": return self.apply("setitem", indexer=indexer, value=value) - def putmask(self, **kwargs): - return self.apply("putmask", **kwargs) + def putmask( + self, mask, new, align: bool = True, axis: int = 0, + ): + transpose = self.ndim == 2 + + return self.apply( + "putmask", + mask=mask, + new=new, + align=align, + inplace=True, + axis=axis, + transpose=transpose, + ) def diff(self, **kwargs) -> "BlockManager": return self.apply("diff", **kwargs) diff --git a/pandas/core/series.py b/pandas/core/series.py index e120695cc83e8..780833d342c06 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2763,7 +2763,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() # ---------------------------------------------------------------------- From 3fcc6e54133e50b979d09b244acd67a904940f64 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 18 Mar 2020 08:21:15 -0700 Subject: [PATCH 2/4] Update pandas/core/internals/blocks.py Co-Authored-By: Simon Hawkins --- pandas/core/internals/blocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 2e5db2dee4e17..4376ba6dc297a 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -941,7 +941,7 @@ def putmask( inplace : bool, default False Perform inplace modification. axis : int - transpose : bool, default False. + transpose : bool, default False Set to True if self is stored with axes reversed. Returns From 2c3ae922f8be13c953b7f47c844851c0b92049e7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 19 Mar 2020 10:46:57 -0700 Subject: [PATCH 3/4] update docstrings --- pandas/core/internals/blocks.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index d58184dac8dcb..dc95b88d38d4a 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -930,7 +930,7 @@ def putmask( putmask the data to the block; it is possible that we may create a new dtype of block - Return the resulting blocks. + Return the resulting block(s). Parameters ---------- @@ -1663,23 +1663,7 @@ def putmask( 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 - align : bool, default True. - Perform alignment on other/cond. - inplace : bool, default False. - Perform inplace modification. - - Returns - ------- - List[Block] + See Block.putmask.__doc__ """ inplace = validate_bool_kwarg(inplace, "inplace") From 67ca8f566b31e16ff333392ae60c081b0f1c9f26 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 21 Mar 2020 18:37:40 -0700 Subject: [PATCH 4/4] merge fixup --- pandas/core/internals/managers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 5e4d911a51d62..b245ac09029a2 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -573,7 +573,6 @@ def putmask( align_keys=align_keys, mask=mask, new=new, - align=align, inplace=True, axis=axis, transpose=transpose,