Skip to content

Commit aae0ae5

Browse files
authored
REF: _setitem_with_indexer_split_path (#37521)
1 parent 99d7fad commit aae0ae5

File tree

1 file changed

+71
-71
lines changed

1 file changed

+71
-71
lines changed

pandas/core/indexing.py

+71-71
Original file line numberDiff line numberDiff line change
@@ -1625,94 +1625,94 @@ def _setitem_with_indexer(self, indexer, value):
16251625
self._setitem_with_indexer_missing(indexer, value)
16261626
return
16271627

1628-
# set
1629-
item_labels = self.obj._get_axis(info_axis)
1630-
16311628
# align and set the values
16321629
if take_split_path:
16331630
# We have to operate column-wise
1631+
self._setitem_with_indexer_split_path(indexer, value)
1632+
else:
1633+
self._setitem_single_block(indexer, value)
16341634

1635-
# Above we only set take_split_path to True for 2D cases
1636-
assert self.ndim == 2
1637-
assert info_axis == 1
1635+
def _setitem_with_indexer_split_path(self, indexer, value):
1636+
"""
1637+
Setitem column-wise.
1638+
"""
1639+
# Above we only set take_split_path to True for 2D cases
1640+
assert self.ndim == 2
16381641

1639-
if not isinstance(indexer, tuple):
1640-
indexer = _tuplify(self.ndim, indexer)
1642+
if not isinstance(indexer, tuple):
1643+
indexer = _tuplify(self.ndim, indexer)
16411644

1642-
if isinstance(value, ABCSeries):
1643-
value = self._align_series(indexer, value)
1645+
if isinstance(value, ABCSeries):
1646+
value = self._align_series(indexer, value)
16441647

1645-
info_idx = indexer[info_axis]
1646-
if is_integer(info_idx):
1647-
info_idx = [info_idx]
1648-
labels = item_labels[info_idx]
1648+
info_idx = indexer[1]
1649+
if is_integer(info_idx):
1650+
info_idx = [info_idx]
1651+
labels = self.obj.columns[info_idx]
16491652

1650-
# Ensure we have something we can iterate over
1651-
ilocs = self._ensure_iterable_column_indexer(indexer[1])
1653+
# Ensure we have something we can iterate over
1654+
ilocs = self._ensure_iterable_column_indexer(indexer[1])
16521655

1653-
plane_indexer = indexer[:1]
1654-
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index)
1655-
# lplane_indexer gives the expected length of obj[indexer[0]]
1656+
plane_indexer = indexer[:1]
1657+
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index)
1658+
# lplane_indexer gives the expected length of obj[indexer[0]]
16561659

1657-
if len(labels) == 1:
1658-
# We can operate on a single column
1660+
if len(labels) == 1:
1661+
# We can operate on a single column
16591662

1660-
# require that we are setting the right number of values that
1661-
# we are indexing
1662-
if is_list_like_indexer(value) and 0 != lplane_indexer != len(value):
1663-
# Exclude zero-len for e.g. boolean masking that is all-false
1664-
raise ValueError(
1665-
"cannot set using a multi-index "
1666-
"selection indexer with a different "
1667-
"length than the value"
1668-
)
1669-
1670-
# we need an iterable, with a ndim of at least 1
1671-
# eg. don't pass through np.array(0)
1672-
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:
1673-
1674-
# we have an equal len Frame
1675-
if isinstance(value, ABCDataFrame):
1676-
self._setitem_with_indexer_frame_value(indexer, value)
1677-
1678-
# we have an equal len ndarray/convertible to our labels
1679-
# hasattr first, to avoid coercing to ndarray without reason.
1680-
# But we may be relying on the ndarray coercion to check ndim.
1681-
# Why not just convert to an ndarray earlier on if needed?
1682-
elif np.ndim(value) == 2:
1683-
self._setitem_with_indexer_2d_value(indexer, value)
1684-
1685-
elif (
1686-
len(labels) == 1
1687-
and lplane_indexer == len(value)
1688-
and not is_scalar(plane_indexer[0])
1689-
):
1690-
# we have an equal len list/ndarray
1691-
# We only get here with len(labels) == len(ilocs) == 1
1692-
self._setitem_single_column(ilocs[0], value, plane_indexer)
1663+
# require that we are setting the right number of values that
1664+
# we are indexing
1665+
if is_list_like_indexer(value) and 0 != lplane_indexer != len(value):
1666+
# Exclude zero-len for e.g. boolean masking that is all-false
1667+
raise ValueError(
1668+
"cannot set using a multi-index "
1669+
"selection indexer with a different "
1670+
"length than the value"
1671+
)
16931672

1694-
elif lplane_indexer == 0 and len(value) == len(self.obj.index):
1695-
# We get here in one case via .loc with a all-False mask
1696-
pass
1673+
# we need an iterable, with a ndim of at least 1
1674+
# eg. don't pass through np.array(0)
1675+
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:
1676+
1677+
# we have an equal len Frame
1678+
if isinstance(value, ABCDataFrame):
1679+
self._setitem_with_indexer_frame_value(indexer, value)
1680+
1681+
# we have an equal len ndarray/convertible to our labels
1682+
# hasattr first, to avoid coercing to ndarray without reason.
1683+
# But we may be relying on the ndarray coercion to check ndim.
1684+
# Why not just convert to an ndarray earlier on if needed?
1685+
elif np.ndim(value) == 2:
1686+
self._setitem_with_indexer_2d_value(indexer, value)
1687+
1688+
elif (
1689+
len(labels) == 1
1690+
and lplane_indexer == len(value)
1691+
and not is_scalar(plane_indexer[0])
1692+
):
1693+
# we have an equal len list/ndarray
1694+
# We only get here with len(labels) == len(ilocs) == 1
1695+
self._setitem_single_column(ilocs[0], value, plane_indexer)
16971696

1698-
else:
1699-
# per-label values
1700-
if len(ilocs) != len(value):
1701-
raise ValueError(
1702-
"Must have equal len keys and value "
1703-
"when setting with an iterable"
1704-
)
1697+
elif lplane_indexer == 0 and len(value) == len(self.obj.index):
1698+
# We get here in one case via .loc with a all-False mask
1699+
pass
17051700

1706-
for loc, v in zip(ilocs, value):
1707-
self._setitem_single_column(loc, v, plane_indexer)
17081701
else:
1702+
# per-label values
1703+
if len(ilocs) != len(value):
1704+
raise ValueError(
1705+
"Must have equal len keys and value "
1706+
"when setting with an iterable"
1707+
)
17091708

1710-
# scalar value
1711-
for loc in ilocs:
1712-
self._setitem_single_column(loc, value, plane_indexer)
1713-
1709+
for loc, v in zip(ilocs, value):
1710+
self._setitem_single_column(loc, v, plane_indexer)
17141711
else:
1715-
self._setitem_single_block(indexer, value)
1712+
1713+
# scalar value
1714+
for loc in ilocs:
1715+
self._setitem_single_column(loc, value, plane_indexer)
17161716

17171717
def _setitem_with_indexer_2d_value(self, indexer, value):
17181718
# We get here with np.ndim(value) == 2, excluding DataFrame,

0 commit comments

Comments
 (0)