Skip to content

BUG: Bug in loc raised ValueError when setting value via boolean list #37761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e38dc1c
Fix listlike loc bug
phofl Nov 11, 2020
e606f33
Add test
phofl Nov 11, 2020
8e02de7
Run black
phofl Nov 11, 2020
c8bacf5
Fix imports
phofl Nov 11, 2020
64c3dfc
Change if condition and move test
phofl Nov 12, 2020
17c0cda
Modify test
phofl Nov 12, 2020
272f53b
Add parametrization
phofl Nov 13, 2020
e37d37c
Convert boolean list for iloc
phofl Nov 13, 2020
9adcd05
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Nov 13, 2020
0a45a73
Parametrize test
phofl Nov 13, 2020
29ec9b5
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Nov 13, 2020
83745b1
Remove if condition part
phofl Nov 13, 2020
7db17ce
Change whatsnew
phofl Nov 13, 2020
a53ae1b
Move test
phofl Nov 13, 2020
b5dd453
Change order of condition
phofl Nov 13, 2020
834c816
Adress review
phofl Nov 19, 2020
da88226
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Nov 19, 2020
1293109
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Dec 21, 2020
99a9330
Move whatsnew
phofl Dec 21, 2020
0f9f515
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Jan 21, 2021
08b0362
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl Jan 28, 2021
b971750
Move validation
phofl Jan 28, 2021
15ad761
Cast to array
phofl Jan 28, 2021
c716b31
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl May 14, 2021
c5c15c9
Merge branch 'master' of https://github.com/pandas-dev/pandas into 20438
phofl May 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ Indexing

- 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`)
- Bug in :meth:`CategoricalIndex.get_indexer` failing to raise ``InvalidIndexError`` when non-unique (:issue:`38372`)
- 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`)
- Bug in inserting many new columns into a :class:`DataFrame` causing incorrect subsequent indexing behavior (:issue:`38380`)
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when setting multiple values to duplicate columns (:issue:`15695`)
- 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`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def check_setitem_lengths(indexer, value, values) -> bool:
if is_list_like(value):
if len(indexer) != len(value) and values.ndim == 1:
# boolean with truth values == len of the value is ok too
if isinstance(indexer, list):
indexer = np.array(indexer)
if not (
isinstance(indexer, np.ndarray)
and indexer.dtype == np.bool_
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,20 @@ def test_iloc_interval(self):
expected = DataFrame({Interval(1, 2): [2, 3]})
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("indexing_func", [list, np.array])
@pytest.mark.parametrize("rhs_func", [list, np.array])
def test_loc_setitem_boolean_list(self, rhs_func, indexing_func):
# GH#20438 testing specifically list key, not arraylike
ser = Series([0, 1, 2])
ser.iloc[indexing_func([True, False, True])] = rhs_func([5, 10])
expected = Series([5, 1, 10])
tm.assert_series_equal(ser, expected)

df = DataFrame({"a": [0, 1, 2]})
df.iloc[indexing_func([True, False, True])] = rhs_func([[5], [10]])
expected = DataFrame({"a": [5, 1, 10]})
tm.assert_frame_equal(df, expected)


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