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

Function putmask_inplace

pandas/core/array_algos/putmask.py:30–60  ·  view source on GitHub ↗

ExtensionArray-compatible implementation of np.putmask. The main difference is we do not handle repeating or truncating like numpy. Parameters ---------- values: np.ndarray or ExtensionArray mask : np.ndarray[bool] We assume extract_bool_array has already been call

(values: ArrayLike, mask: npt.NDArray[np.bool_], value: Any)

Source from the content-addressed store, hash-verified

28
29
30def putmask_inplace(values: ArrayLike, mask: npt.NDArray[np.bool_], value: Any) -> None:
31 """
32 ExtensionArray-compatible implementation of np.putmask. The main
33 difference is we do not handle repeating or truncating like numpy.
34
35 Parameters
36 ----------
37 values: np.ndarray or ExtensionArray
38 mask : np.ndarray[bool]
39 We assume extract_bool_array has already been called.
40 value : Any
41 """
42
43 if (
44 not isinstance(values, np.ndarray)
45 or (values.dtype == object and not lib.is_scalar(value))
46 # GH#43424: np.putmask raises TypeError if we cannot cast between types with
47 # rule = "safe", a stricter guarantee we may not have here
48 or (
49 isinstance(value, np.ndarray) and not np.can_cast(value.dtype, values.dtype)
50 )
51 ):
52 # GH#19266 using np.putmask gives unexpected results with listlike value
53 # along with object dtype
54 if is_list_like(value) and len(value) == len(values):
55 values[mask] = value[mask]
56 else:
57 values[mask] = value
58 else:
59 # GH#37833 np.putmask is more performant than __setitem__
60 np.putmask(values, mask, value)
61
62
63def putmask_without_repeat(

Callers 2

replaceMethod · 0.90
_replace_coerceMethod · 0.90

Calls 2

is_scalarMethod · 0.45
putmaskMethod · 0.45

Tested by

no test coverage detected