Skip to content

Commit

Permalink
BUG: loc.setitem corner case (#37931)
Browse files Browse the repository at this point in the history
* BUG: loc.setitem corner case

* flesh out comment
  • Loading branch information
jbrockmendel authored Nov 26, 2020
1 parent fa999f3 commit fe87624
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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])

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]
assert (df.loc[4, "c"] == 0).all()

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

0 comments on commit fe87624

Please sign in to comment.