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

Method putmask

pandas/core/internals/blocks.py:1753–1812  ·  view source on GitHub ↗

See Block.putmask.__doc__

(self, mask, new)

Source from the content-addressed store, hash-verified

1751
1752 @final
1753 def putmask(self, mask, new) -> list[Block]:
1754 """
1755 See Block.putmask.__doc__
1756 """
1757 mask = extract_bool_array(mask)
1758 if new is lib.no_default:
1759 new = self.fill_value
1760
1761 orig_new = new
1762 orig_mask = mask
1763 new = self._maybe_squeeze_arg(new)
1764 mask = self._maybe_squeeze_arg(mask)
1765
1766 if not mask.any():
1767 return [self.copy(deep=False)]
1768
1769 self = self._maybe_copy(inplace=True)
1770 values = self.values
1771 if values.ndim == 2:
1772 values = values.T
1773
1774 try:
1775 # Caller is responsible for ensuring matching lengths
1776 values._putmask(mask, new)
1777 except OutOfBoundsDatetime:
1778 raise
1779 except (TypeError, ValueError):
1780 if self.ndim == 1 or self.shape[0] == 1:
1781 if isinstance(self.dtype, IntervalDtype):
1782 # Discussion about what we want to support in the general
1783 # case GH#39584
1784 blk = self.coerce_to_target_dtype(orig_new, raise_on_upcast=True)
1785 return blk.putmask(orig_mask, orig_new)
1786
1787 elif isinstance(self, NDArrayBackedExtensionBlock):
1788 # NB: not (yet) the same as
1789 # isinstance(values, NDArrayBackedExtensionArray)
1790 blk = self.coerce_to_target_dtype(orig_new, raise_on_upcast=True)
1791 return blk.putmask(orig_mask, orig_new)
1792
1793 else:
1794 raise
1795
1796 else:
1797 # Same pattern we use in Block.putmask
1798 is_array = isinstance(orig_new, (np.ndarray, ExtensionArray))
1799
1800 res_blocks = []
1801 for i, nb in enumerate(self._split()):
1802 n = orig_new
1803 if is_array:
1804 # we have a different value per-column
1805 n = orig_new[:, i : i + 1]
1806
1807 submask = orig_mask[:, i : i + 1]
1808 rbs = nb.putmask(submask, n)
1809 res_blocks.extend(rbs)
1810 return res_blocks

Callers

nothing calls this directly

Calls 9

extract_bool_arrayFunction · 0.90
_maybe_copyMethod · 0.80
_splitMethod · 0.80
_maybe_squeeze_argMethod · 0.45
anyMethod · 0.45
copyMethod · 0.45
_putmaskMethod · 0.45
putmaskMethod · 0.45

Tested by

no test coverage detected