Skip to content

Commit e4d0c1d

Browse files
authored
BUG: Bug in loc raised ValueError when setting value via boolean list (#37761)
1 parent 8327872 commit e4d0c1d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ Indexing
845845

846846
- Bug in :meth:`Index.union` dropping duplicate ``Index`` values when ``Index`` was not monotonic or ``sort`` was set to ``False`` (:issue:`36289`, :issue:`31326`, :issue:`40862`)
847847
- Bug in :meth:`CategoricalIndex.get_indexer` failing to raise ``InvalidIndexError`` when non-unique (:issue:`38372`)
848+
- Bug in :meth:`Series.loc` raising ``ValueError`` when input was filtered with a boolean list and values to set were a list with lower dimension (:issue:`20438`)
848849
- Bug in inserting many new columns into a :class:`DataFrame` causing incorrect subsequent indexing behavior (:issue:`38380`)
849850
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when setting multiple values to duplicate columns (:issue:`15695`)
850851
- Bug in :meth:`DataFrame.loc`, :meth:`Series.loc`, :meth:`DataFrame.__getitem__` and :meth:`Series.__getitem__` returning incorrect elements for non-monotonic :class:`DatetimeIndex` for string slices (:issue:`33146`)

pandas/core/indexers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def check_setitem_lengths(indexer, value, values) -> bool:
166166
if is_list_like(value):
167167
if len(indexer) != len(value) and values.ndim == 1:
168168
# boolean with truth values == len of the value is ok too
169+
if isinstance(indexer, list):
170+
indexer = np.array(indexer)
169171
if not (
170172
isinstance(indexer, np.ndarray)
171173
and indexer.dtype == np.bool_

pandas/tests/indexing/test_iloc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,20 @@ def test_iloc_interval(self):
11141114
expected = DataFrame({Interval(1, 2): [2, 3]})
11151115
tm.assert_frame_equal(result, expected)
11161116

1117+
@pytest.mark.parametrize("indexing_func", [list, np.array])
1118+
@pytest.mark.parametrize("rhs_func", [list, np.array])
1119+
def test_loc_setitem_boolean_list(self, rhs_func, indexing_func):
1120+
# GH#20438 testing specifically list key, not arraylike
1121+
ser = Series([0, 1, 2])
1122+
ser.iloc[indexing_func([True, False, True])] = rhs_func([5, 10])
1123+
expected = Series([5, 1, 10])
1124+
tm.assert_series_equal(ser, expected)
1125+
1126+
df = DataFrame({"a": [0, 1, 2]})
1127+
df.iloc[indexing_func([True, False, True])] = rhs_func([[5], [10]])
1128+
expected = DataFrame({"a": [5, 1, 10]})
1129+
tm.assert_frame_equal(df, expected)
1130+
11171131

11181132
class TestILocErrors:
11191133
# NB: this test should work for _any_ Series we can pass as

0 commit comments

Comments
 (0)