Skip to content

BUG: loc.setitem corner case #37931

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 3 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,8 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
value = self._align_series(indexer, value)

# Ensure we have something we can iterate over
ilocs = self._ensure_iterable_column_indexer(indexer[1])
info_axis = indexer[1]
ilocs = self._ensure_iterable_column_indexer(info_axis)

pi = indexer[0]
lplane_indexer = length_of_indexer(pi, self.obj.index)
Expand All @@ -1673,6 +1674,12 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
# We are trying to set N values into M entries of a single
# column, which is invalid for N != M
# Exclude zero-len for e.g. boolean masking that is all-false

if len(value) == 1 and not is_integer(info_axis):
# This is a case like df.iloc[:3, [1]] = [0]
# where we treat as df.iloc[:3, 1] = 0
return self._setitem_with_indexer((pi, info_axis[0]), value[0])
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 reason for not using _setitem_single_column? Seems to work when trying out locally. LGTM otherwise

Copy link
Member Author

Choose a reason for hiding this comment

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

either way works. this seemed like a more obvious "we are reducing this to a simpler case" representation

Copy link
Member

Choose a reason for hiding this comment

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

Ok, this is fine with me then.


raise ValueError(
"Must have equal len keys and value "
"when setting with an iterable"
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexing/multiindex/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ def test_multiindex_assignment(self):
with pytest.raises(ValueError, match=msg):
df.loc[4, "c"] = [0]

# But with a length-1 listlike column indexer this behaves like
# `df.loc[4, "c"] = 0
df.loc[4, ["c"]] = [0]
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens with this case now?

Copy link
Member Author

Choose a reason for hiding this comment

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

behaves like df.loc[4, "c"] = 0, just edited the comment to make that clear

assert (df.loc[4, "c"] == 0).all()

def test_groupby_example(self):
# groupby example
NUM_ROWS = 100
NUM_COLS = 10
Expand Down