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

Method where

pandas/core/internals/blocks.py:1687–1750  ·  view source on GitHub ↗
(self, other, cond)

Source from the content-addressed store, hash-verified

1685
1686 @final
1687 def where(self, other, cond) -> list[Block]:
1688 arr = self.values.T
1689
1690 cond = extract_bool_array(cond)
1691
1692 orig_other = other
1693 orig_cond = cond
1694 other = self._maybe_squeeze_arg(other)
1695 cond = self._maybe_squeeze_arg(cond)
1696
1697 if other is lib.no_default:
1698 other = self.fill_value
1699
1700 icond, noop = validate_putmask(arr, ~cond)
1701 if noop:
1702 # GH#44181, GH#45135
1703 # Avoid a) raising for Interval/PeriodDtype and b) unnecessary object upcast
1704 return [self.copy(deep=False)]
1705
1706 try:
1707 res_values = arr._where(cond, other).T
1708 except OutOfBoundsDatetime:
1709 raise
1710 except (ValueError, TypeError):
1711 if self.ndim == 1 or self.shape[0] == 1:
1712 if isinstance(self.dtype, (IntervalDtype, StringDtype)):
1713 # TestSetitemFloatIntervalWithIntIntervalValues
1714 blk = self.coerce_to_target_dtype(orig_other, raise_on_upcast=False)
1715 if (
1716 self.ndim == 2
1717 and isinstance(orig_cond, np.ndarray)
1718 and orig_cond.ndim == 1
1719 and not is_1d_only_ea_dtype(blk.dtype)
1720 ):
1721 orig_cond = orig_cond[:, None]
1722 return blk.where(orig_other, orig_cond)
1723
1724 elif isinstance(self, NDArrayBackedExtensionBlock):
1725 # NB: not (yet) the same as
1726 # isinstance(values, NDArrayBackedExtensionArray)
1727 blk = self.coerce_to_target_dtype(orig_other, raise_on_upcast=False)
1728 return blk.where(orig_other, orig_cond)
1729
1730 else:
1731 raise
1732
1733 else:
1734 # Same pattern we use in Block.putmask
1735 is_array = isinstance(orig_other, (np.ndarray, ExtensionArray))
1736
1737 res_blocks = []
1738 for i, nb in enumerate(self._split()):
1739 n = orig_other
1740 if is_array:
1741 # we have a different value per-column
1742 n = orig_other[:, i : i + 1]
1743
1744 submask = orig_cond[:, i : i + 1]

Callers

nothing calls this directly

Calls 10

extract_bool_arrayFunction · 0.90
validate_putmaskFunction · 0.90
is_1d_only_ea_dtypeFunction · 0.90
_splitMethod · 0.80
make_block_same_classMethod · 0.80
_maybe_squeeze_argMethod · 0.45
copyMethod · 0.45
_whereMethod · 0.45
whereMethod · 0.45

Tested by

no test coverage detected