Skip to content

Commit 12cea49

Browse files
authored
CLN: simplify Block.putmask (#38677)
1 parent d8b5fac commit 12cea49

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed

pandas/core/internals/blocks.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,7 @@ def _putmask_simple(self, mask: np.ndarray, value: Any):
10321032
# GH#37833 np.putmask is more performant than __setitem__
10331033
np.putmask(values, mask, value)
10341034

1035-
def putmask(
1036-
self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False
1037-
) -> List["Block"]:
1035+
def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
10381036
"""
10391037
putmask the data to the block; it is possible that we may create a
10401038
new dtype of block
@@ -1045,16 +1043,13 @@ def putmask(
10451043
----------
10461044
mask : np.ndarray[bool], SparseArray[bool], or BooleanArray
10471045
new : a ndarray/object
1048-
inplace : bool, default False
1049-
Perform inplace modification.
10501046
axis : int
1051-
transpose : bool, default False
1052-
Set to True if self is stored with axes reversed.
10531047
10541048
Returns
10551049
-------
10561050
List[Block]
10571051
"""
1052+
transpose = self.ndim == 2
10581053
mask = _extract_bool_array(mask)
10591054
assert not isinstance(new, (ABCIndex, ABCSeries, ABCDataFrame))
10601055

@@ -1091,8 +1086,6 @@ def putmask(
10911086
new = np.repeat(new, new_values.shape[-1]).reshape(self.shape)
10921087
new = new.astype(new_values.dtype)
10931088

1094-
if new_values is self.values and not inplace:
1095-
new_values = new_values.copy()
10961089
# we require exact matches between the len of the
10971090
# values we are setting (or is compat). np.putmask
10981091
# doesn't check this and will simply truncate / pad
@@ -1113,6 +1106,7 @@ def putmask(
11131106
# `np.place` on the other hand uses the ``new`` values at it is
11141107
# to place in the masked locations of ``new_values``
11151108
np.place(new_values, mask, new)
1109+
# i.e. new_values[mask] = new
11161110
elif mask.shape[-1] == len(new) or len(new) == 1:
11171111
np.putmask(new_values, mask, new)
11181112
else:
@@ -1157,18 +1151,10 @@ def f(mask, val, idx):
11571151
nv = _putmask_smart(val, mask, n)
11581152
return nv
11591153

1160-
new_blocks = self.split_and_operate(mask, f, inplace)
1154+
new_blocks = self.split_and_operate(mask, f, True)
11611155
return new_blocks
11621156

1163-
if inplace:
1164-
return [self]
1165-
1166-
if transpose:
1167-
if new_values is None:
1168-
new_values = self.values if inplace else self.values.copy()
1169-
new_values = new_values.T
1170-
1171-
return [self.make_block(new_values)]
1157+
return [self]
11721158

11731159
def coerce_to_target_dtype(self, other):
11741160
"""
@@ -1711,17 +1697,13 @@ def set_inplace(self, locs, values):
17111697
assert locs.tolist() == [0]
17121698
self.values = values
17131699

1714-
def putmask(
1715-
self, mask, new, inplace: bool = False, axis: int = 0, transpose: bool = False
1716-
) -> List["Block"]:
1700+
def putmask(self, mask, new, axis: int = 0) -> List["Block"]:
17171701
"""
17181702
See Block.putmask.__doc__
17191703
"""
1720-
inplace = validate_bool_kwarg(inplace, "inplace")
1721-
17221704
mask = _extract_bool_array(mask)
17231705

1724-
new_values = self.values if inplace else self.values.copy()
1706+
new_values = self.values
17251707

17261708
if isinstance(new, (np.ndarray, ExtensionArray)) and len(new) == len(mask):
17271709
new = new[mask]

pandas/core/internals/managers.py

-3
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ def setitem(self, indexer, value) -> "BlockManager":
565565
return self.apply("setitem", indexer=indexer, value=value)
566566

567567
def putmask(self, mask, new, align: bool = True, axis: int = 0):
568-
transpose = self.ndim == 2
569568

570569
if align:
571570
align_keys = ["new", "mask"]
@@ -578,9 +577,7 @@ def putmask(self, mask, new, align: bool = True, axis: int = 0):
578577
align_keys=align_keys,
579578
mask=mask,
580579
new=new,
581-
inplace=True,
582580
axis=axis,
583-
transpose=transpose,
584581
)
585582

586583
def diff(self, n: int, axis: int) -> "BlockManager":

0 commit comments

Comments
 (0)