-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 9 commits
e38dc1c
e606f33
8e02de7
c8bacf5
64c3dfc
17c0cda
272f53b
e37d37c
9adcd05
0a45a73
29ec9b5
83745b1
7db17ce
a53ae1b
b5dd453
834c816
da88226
1293109
99a9330
0f9f515
08b0362
b971750
15ad761
c716b31
c5c15c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,7 +602,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 com.is_bool_indexer(key): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i haven't fully formed an opinion, but i think another alternative would be to properly handle boolean masks in Block.setitem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, loc is not failing, this means we do not need this or. Removed it. Must have mixed something up before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lists can be handled in Block.setitem already, but arrays from dtype object don't work. Lists are killed by the validation mentioned above in |
||
self._ensure_listlike_indexer(key) | ||
|
||
if self.axis is not None: | ||
|
@@ -1521,6 +1521,8 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): | |
|
||
def _get_setitem_indexer(self, key): | ||
# GH#32257 Fall through to let numpy do validation | ||
if com.is_bool_indexer(key) and isinstance(key, list): | ||
return np.array(key) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return key | ||
|
||
# ------------------------------------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1645,6 +1645,20 @@ def test_loc_setitem_mask_td64_series_value(self): | |
assert expected == result | ||
tm.assert_frame_equal(df, df_copy) | ||
|
||
@pytest.mark.parametrize("func", ["loc", "iloc"]) | ||
@pytest.mark.parametrize("rhs_func", [list, np.array]) | ||
def test_loc_setitem_boolean_list(self, func, rhs_func): | ||
# GH#20438 testing specifically list key, not arraylike | ||
ser = Series([0, 1, 2]) | ||
getattr(ser, func)[[True, False, True]] = rhs_func([5, 10]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also parameterize on the indexing type as well (e.g. list / array / pd.Index) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Index is not allowed for iloc, added the remaining ones |
||
expected = Series([5, 1, 10]) | ||
tm.assert_series_equal(ser, expected) | ||
|
||
df = DataFrame({"a": [0, 1, 2]}) | ||
getattr(df, func)[[True, False, True]] = rhs_func([[5], [10]]) | ||
expected = DataFrame({"a": [5, 1, 10]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
class TestLocListlike: | ||
@pytest.mark.parametrize("box", [lambda x: x, np.asarray, list]) | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ValueError, added it