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

Method setitem

pandas/core/internals/blocks.py:1632–1684  ·  view source on GitHub ↗

Attempt self.values[indexer] = value, possibly creating a new array. This differs from Block.setitem by not allowing setitem to change the dtype of the Block. Parameters ---------- indexer : tuple, list-like, array-like, slice, int The s

(self, indexer, value)

Source from the content-addressed store, hash-verified

1630
1631 @final
1632 def setitem(self, indexer, value):
1633 """
1634 Attempt self.values[indexer] = value, possibly creating a new array.
1635
1636 This differs from Block.setitem by not allowing setitem to change
1637 the dtype of the Block.
1638
1639 Parameters
1640 ----------
1641 indexer : tuple, list-like, array-like, slice, int
1642 The subset of self.values to set
1643 value : object
1644 The value being set
1645
1646 Returns
1647 -------
1648 Block
1649
1650 Notes
1651 -----
1652 `indexer` is a direct slice/positional indexer. `value` must
1653 be a compatible shape.
1654 """
1655 orig_indexer = indexer
1656 orig_value = value
1657
1658 indexer = self._unwrap_setitem_indexer(indexer)
1659 value = self._maybe_squeeze_arg(value)
1660
1661 values = self.values
1662 if values.ndim == 2:
1663 # TODO(GH#45419): string[pyarrow] tests break if we transpose
1664 # unconditionally
1665 values = values.T
1666 check_setitem_lengths(indexer, value, values)
1667
1668 try:
1669 values[indexer] = value
1670 except (ValueError, TypeError):
1671 if isinstance(self.dtype, IntervalDtype):
1672 # see TestSetitemFloatIntervalWithIntIntervalValues
1673 nb = self.coerce_to_target_dtype(orig_value, raise_on_upcast=True)
1674 return nb.setitem(orig_indexer, orig_value)
1675
1676 elif isinstance(self, NDArrayBackedExtensionBlock):
1677 nb = self.coerce_to_target_dtype(orig_value, raise_on_upcast=True)
1678 return nb.setitem(orig_indexer, orig_value)
1679
1680 else:
1681 raise
1682
1683 else:
1684 return self
1685
1686 @final
1687 def where(self, other, cond) -> list[Block]:

Callers

nothing calls this directly

Calls 5

check_setitem_lengthsFunction · 0.90
_maybe_squeeze_argMethod · 0.45
setitemMethod · 0.45

Tested by

no test coverage detected