Skip to content

Commit 60269d9

Browse files
authored
TYP: annotate Block/BlockManager putmask (#32769)
1 parent 8f90046 commit 60269d9

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

pandas/core/generic.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -8652,12 +8652,7 @@ def _where(
86528652

86538653
self._check_inplace_setting(other)
86548654
new_data = self._data.putmask(
8655-
mask=cond,
8656-
new=other,
8657-
align=align,
8658-
inplace=True,
8659-
axis=block_axis,
8660-
transpose=self._AXIS_REVERSED,
8655+
mask=cond, new=other, align=align, axis=block_axis,
86618656
)
86628657
self._update_inplace(new_data)
86638658

pandas/core/internals/blocks.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -910,25 +910,26 @@ def setitem(self, indexer, value):
910910

911911
def putmask(
912912
self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False,
913-
):
913+
) -> List["Block"]:
914914
"""
915915
putmask the data to the block; it is possible that we may create a
916916
new dtype of block
917917
918-
return the resulting block(s)
918+
Return the resulting block(s).
919919
920920
Parameters
921921
----------
922-
mask : the condition to respect
922+
mask : the condition to respect
923923
new : a ndarray/object
924-
inplace : perform inplace modification, default is False
924+
inplace : bool, default False
925+
Perform inplace modification.
925926
axis : int
926-
transpose : boolean
927-
Set to True if self is stored with axes reversed
927+
transpose : bool, default False
928+
Set to True if self is stored with axes reversed.
928929
929930
Returns
930931
-------
931-
a list of new blocks, the result of the putmask
932+
List[Block]
932933
"""
933934
new_values = self.values if inplace else self.values.copy()
934935

@@ -1626,23 +1627,10 @@ def set(self, locs, values):
16261627
self.values[:] = values
16271628

16281629
def putmask(
1629-
self, mask, new, inplace=False, axis=0, transpose=False,
1630-
):
1630+
self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False,
1631+
) -> List["Block"]:
16311632
"""
1632-
putmask the data to the block; we must be a single block and not
1633-
generate other blocks
1634-
1635-
return the resulting block
1636-
1637-
Parameters
1638-
----------
1639-
mask : the condition to respect
1640-
new : a ndarray/object
1641-
inplace : perform inplace modification, default is False
1642-
1643-
Returns
1644-
-------
1645-
a new block, the result of the putmask
1633+
See Block.putmask.__doc__
16461634
"""
16471635
inplace = validate_bool_kwarg(inplace, "inplace")
16481636

pandas/core/internals/managers.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,25 @@ def where(self, **kwargs) -> "BlockManager":
558558
def setitem(self, indexer, value) -> "BlockManager":
559559
return self.apply("setitem", indexer=indexer, value=value)
560560

561-
def putmask(self, **kwargs):
561+
def putmask(
562+
self, mask, new, align: bool = True, axis: int = 0,
563+
):
564+
transpose = self.ndim == 2
562565

563-
if kwargs.pop("align", True):
566+
if align:
564567
align_keys = ["new", "mask"]
565568
else:
566569
align_keys = ["mask"]
567570

568-
return self.apply("putmask", align_keys=align_keys, **kwargs)
571+
return self.apply(
572+
"putmask",
573+
align_keys=align_keys,
574+
mask=mask,
575+
new=new,
576+
inplace=True,
577+
axis=axis,
578+
transpose=transpose,
579+
)
569580

570581
def diff(self, n: int, axis: int) -> "BlockManager":
571582
return self.apply("diff", n=n, axis=axis)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ def update(self, other) -> None:
28122812
other = other.reindex_like(self)
28132813
mask = notna(other)
28142814

2815-
self._data = self._data.putmask(mask=mask, new=other, inplace=True)
2815+
self._data = self._data.putmask(mask=mask, new=other)
28162816
self._maybe_update_cacher()
28172817

28182818
# ----------------------------------------------------------------------

0 commit comments

Comments
 (0)