MCPcopy
hub / github.com/pandas-dev/pandas / putmask

Method putmask

pandas/core/internals/blocks.py:1144–1209  ·  view source on GitHub ↗

putmask the data to the block; it is possible that we may create a new dtype of block Return the resulting block(s). Parameters ---------- mask : np.ndarray[bool], SparseArray[bool], or BooleanArray new : an ndarray/object Returns

(self, mask, new)

Source from the content-addressed store, hash-verified

1142 return self
1143
1144 def putmask(self, mask, new) -> list[Block]:
1145 """
1146 putmask the data to the block; it is possible that we may create a
1147 new dtype of block
1148
1149 Return the resulting block(s).
1150
1151 Parameters
1152 ----------
1153 mask : np.ndarray[bool], SparseArray[bool], or BooleanArray
1154 new : an ndarray/object
1155
1156 Returns
1157 -------
1158 List[Block]
1159 """
1160 orig_mask = mask
1161 values = cast(np.ndarray, self.values)
1162 mask, noop = validate_putmask(values.T, mask)
1163 assert not isinstance(new, (ABCIndex, ABCSeries, ABCDataFrame))
1164
1165 if new is lib.no_default:
1166 new = self.fill_value
1167
1168 new = self._standardize_fill_value(new)
1169 new = extract_array(new, extract_numpy=True)
1170
1171 if noop:
1172 return [self.copy(deep=False)]
1173
1174 try:
1175 casted = np_can_hold_element(values.dtype, new)
1176
1177 self = self._maybe_copy(inplace=True)
1178 values = cast(np.ndarray, self.values)
1179
1180 putmask_without_repeat(values.T, mask, casted)
1181 return [self]
1182 except LossySetitemError:
1183 if self.ndim == 1 or self.shape[0] == 1:
1184 # no need to split columns
1185
1186 if not is_list_like(new):
1187 # using just new[indexer] can't save us the need to cast
1188 return self.coerce_to_target_dtype(
1189 new, raise_on_upcast=True
1190 ).putmask(mask, new)
1191 else:
1192 indexer = mask.nonzero()[0]
1193 nb = self.setitem(indexer, new[indexer])
1194 return [nb]
1195
1196 else:
1197 is_array = isinstance(new, np.ndarray)
1198
1199 res_blocks = []
1200 for i, nb in enumerate(self._split()):
1201 n = new

Callers 3

fillnaMethod · 0.95
whereMethod · 0.45
putmaskMethod · 0.45

Calls 11

copyMethod · 0.95
_maybe_copyMethod · 0.95
setitemMethod · 0.95
_splitMethod · 0.95
validate_putmaskFunction · 0.90
extract_arrayFunction · 0.90
np_can_hold_elementFunction · 0.90
putmask_without_repeatFunction · 0.90
nonzeroMethod · 0.80

Tested by

no test coverage detected