Skip to content

Commit ad717c7

Browse files
authored
Bug in setitem raising ValueError with row-slice indexer on df with list on rhs (#41268)
1 parent 5f39b51 commit ad717c7

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ Indexing
752752
- Bug in :meth:`RangeIndex.append` where a single object of length 1 was concatenated incorrectly (:issue:`39401`)
753753
- Bug in setting ``numpy.timedelta64`` values into an object-dtype :class:`Series` using a boolean indexer (:issue:`39488`)
754754
- Bug in setting numeric values into a into a boolean-dtypes :class:`Series` using ``at`` or ``iat`` failing to cast to object-dtype (:issue:`39582`)
755+
- Bug in :meth:`DataFrame.__setitem__` and :meth:`DataFrame.iloc.__setitem__` raising ``ValueError`` when trying to index with a row-slice and setting a list as values (:issue:`40440`)
755756
- Bug in :meth:`DataFrame.loc.__setitem__` when setting-with-expansion incorrectly raising when the index in the expanding axis contains duplicates (:issue:`40096`)
756757
- Bug in :meth:`DataFrame.loc` incorrectly matching non-boolean index elements (:issue:`20432`)
757758
- Bug in :meth:`Series.__delitem__` with ``ExtensionDtype`` incorrectly casting to ``ndarray`` (:issue:`40386`)

pandas/core/indexers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def check_setitem_lengths(indexer, value, values) -> bool:
180180

181181
elif isinstance(indexer, slice):
182182
if is_list_like(value):
183-
if len(value) != length_of_indexer(indexer, values):
183+
if len(value) != length_of_indexer(indexer, values) and values.ndim == 1:
184+
# In case of two dimensional value is used row-wise and broadcasted
184185
raise ValueError(
185186
"cannot set using a slice indexer with a "
186187
"different length than the value"

pandas/tests/frame/indexing/test_setitem.py

+28
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,34 @@ def test_setitem_slice_position(self):
791791
expected = DataFrame(arr)
792792
tm.assert_frame_equal(df, expected)
793793

794+
@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
795+
@pytest.mark.parametrize("box", [Series, np.array, list])
796+
@pytest.mark.parametrize("n", [1, 2, 3])
797+
def test_setitem_broadcasting_rhs(self, n, box, indexer):
798+
# GH#40440
799+
# TODO: Add pandas array as box after GH#40933 is fixed
800+
df = DataFrame([[1, 3, 5]] + [[2, 4, 6]] * n, columns=["a", "b", "c"])
801+
indexer(df)[1:] = box([10, 11, 12])
802+
expected = DataFrame([[1, 3, 5]] + [[10, 11, 12]] * n, columns=["a", "b", "c"])
803+
tm.assert_frame_equal(df, expected)
804+
805+
@pytest.mark.parametrize("indexer", [tm.setitem, tm.iloc])
806+
@pytest.mark.parametrize("box", [Series, np.array, list])
807+
@pytest.mark.parametrize("n", [1, 2, 3])
808+
def test_setitem_broadcasting_rhs_mixed_dtypes(self, n, box, indexer):
809+
# GH#40440
810+
# TODO: Add pandas array as box after GH#40933 is fixed
811+
df = DataFrame(
812+
[[1, 3, 5], ["x", "y", "z"]] + [[2, 4, 6]] * n, columns=["a", "b", "c"]
813+
)
814+
indexer(df)[1:] = box([10, 11, 12])
815+
expected = DataFrame(
816+
[[1, 3, 5]] + [[10, 11, 12]] * (n + 1),
817+
columns=["a", "b", "c"],
818+
dtype="object",
819+
)
820+
tm.assert_frame_equal(df, expected)
821+
794822

795823
class TestDataFrameSetItemCallable:
796824
def test_setitem_callable(self):

0 commit comments

Comments
 (0)