@@ -1647,6 +1647,8 @@ def _setitem_with_indexer_split_path(self, indexer, value):
1647
1647
indexer = _tuplify (self .ndim , indexer )
1648
1648
if len (indexer ) > self .ndim :
1649
1649
raise IndexError ("too many indices for array" )
1650
+ if isinstance (indexer [0 ], np .ndarray ) and indexer [0 ].ndim > 2 :
1651
+ raise ValueError (r"Cannot set values with ndim > 2" )
1650
1652
1651
1653
if isinstance (value , ABCSeries ):
1652
1654
value = self ._align_series (indexer , value )
@@ -1658,57 +1660,45 @@ def _setitem_with_indexer_split_path(self, indexer, value):
1658
1660
lplane_indexer = length_of_indexer (pi , self .obj .index )
1659
1661
# lplane_indexer gives the expected length of obj[indexer[0]]
1660
1662
1661
- if len (ilocs ) == 1 :
1662
- # We can operate on a single column
1663
-
1664
- # require that we are setting the right number of values that
1665
- # we are indexing
1666
- if is_list_like_indexer (value ) and 0 != lplane_indexer != len (value ):
1667
- # Exclude zero-len for e.g. boolean masking that is all-false
1668
- raise ValueError (
1669
- "cannot set using a multi-index "
1670
- "selection indexer with a different "
1671
- "length than the value"
1672
- )
1673
-
1674
1663
# we need an iterable, with a ndim of at least 1
1675
1664
# eg. don't pass through np.array(0)
1676
1665
if is_list_like_indexer (value ) and getattr (value , "ndim" , 1 ) > 0 :
1677
1666
1678
- # we have an equal len Frame
1679
1667
if isinstance (value , ABCDataFrame ):
1680
1668
self ._setitem_with_indexer_frame_value (indexer , value )
1681
1669
1682
- # we have an equal len ndarray/convertible to our ilocs
1683
- # hasattr first, to avoid coercing to ndarray without reason.
1684
- # But we may be relying on the ndarray coercion to check ndim.
1685
- # Why not just convert to an ndarray earlier on if needed?
1686
1670
elif np .ndim (value ) == 2 :
1687
1671
self ._setitem_with_indexer_2d_value (indexer , value )
1688
1672
1689
1673
elif len (ilocs ) == 1 and lplane_indexer == len (value ) and not is_scalar (pi ):
1690
- # we have an equal len list/ndarray
1691
- # We only get here with len(ilocs) == 1
1674
+ # We are setting multiple rows in a single column.
1692
1675
self ._setitem_single_column (ilocs [0 ], value , pi )
1693
1676
1677
+ elif len (ilocs ) == 1 and 0 != lplane_indexer != len (value ):
1678
+ # We are trying to set N values into M entries of a single
1679
+ # column, which is invalid for N != M
1680
+ # Exclude zero-len for e.g. boolean masking that is all-false
1681
+ raise ValueError (
1682
+ "Must have equal len keys and value "
1683
+ "when setting with an iterable"
1684
+ )
1685
+
1694
1686
elif lplane_indexer == 0 and len (value ) == len (self .obj .index ):
1695
1687
# We get here in one case via .loc with a all-False mask
1696
1688
pass
1697
1689
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
- )
1705
-
1690
+ elif len (ilocs ) == len (value ):
1691
+ # We are setting multiple columns in a single row.
1706
1692
for loc , v in zip (ilocs , value ):
1707
1693
self ._setitem_single_column (loc , v , pi )
1708
- else :
1709
1694
1710
- if isinstance (indexer [0 ], np .ndarray ) and indexer [0 ].ndim > 2 :
1711
- raise ValueError (r"Cannot set values with ndim > 2" )
1695
+ else :
1696
+ raise ValueError (
1697
+ "Must have equal len keys and value "
1698
+ "when setting with an iterable"
1699
+ )
1700
+
1701
+ else :
1712
1702
1713
1703
# scalar value
1714
1704
for loc in ilocs :
0 commit comments