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 4 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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ Indexing
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` raises when numeric label was given for object :class:`Index` although label was in :class:`Index` (:issue:`26491`)
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from :class:`MultiIndex` (:issue:`27104`)
- Bug in :meth:`Series.loc` raised an Error when input was filtered with a boolean list and values to set were a list with lower dimension (:issue:`20438`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a specific exception we can say instead of Error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError, added it


Missing
^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
is_hashable,
is_integer,
is_iterator,
Expand Down Expand Up @@ -602,7 +603,7 @@ def _get_setitem_indexer(self, key):
"""
Convert a potentially-label-based key into a positional indexer.
"""
if self.name == "loc":
if self.name == "loc" or is_bool_dtype(np.array(key)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it only lists we want here, or would com.is_bool_indexer(key) work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is better, changed it :)

self._ensure_listlike_indexer(key)

if self.axis is not None:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,13 @@ def test_loc_setitem_dt64tz_values(self):
result = s2["a"]
assert result == expected

def test_loc_setitem_boolean_list(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets put this in TestLocBooleanMask

can this be shared by DataFrame?

few words after the GH ref to the effect of "specifically list key, not arraylike"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added a DataFrame part to the test, parametrization would be quite ugly here

# GH: 20438
ser = Series([0, 1, 2])
ser.loc[[True, False, True]] = [5, 10]
expected = Series([5, 1, 10])
tm.assert_series_equal(ser, expected)


@pytest.mark.parametrize("value", [1, 1.5])
def test_loc_int_in_object_index(frame_or_series, value):
Expand Down