Skip to content

Commit bd8f31e

Browse files
committed
BUG: bug in setitem with multi-index and a 0-dim ndarray (GH7218)
1 parent e8d8c61 commit bd8f31e

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ Bug Fixes
522522
- Bug in ``query``/``eval`` where global constants were not looked up correctly
523523
(:issue:`7178`)
524524
- Bug in recognizing out-of-bounds positional list indexers with ``iloc`` and a multi-axis tuple indexer (:issue:`7189`)
525-
- Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`)
525+
- Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`, :issue:`7218`)
526526
- Bug in expressions evaluation with reversed ops, showing in series-dataframe ops (:issue:`7198`, :issue:`7192`)
527527
- Bug in multi-axis indexing with > 2 ndim and a multi-index (:issue:`7199`)
528528

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _setitem_with_indexer(self, indexer, value):
339339

340340
# require that we are setting the right number of values that
341341
# we are indexing
342-
if is_list_like(value) and lplane_indexer != len(value):
342+
if is_list_like(value) and np.iterable(value) and lplane_indexer != len(value):
343343

344344
if len(obj[idx]) != len(value):
345345
raise ValueError(
@@ -386,7 +386,7 @@ def setter(item, v):
386386

387387
def can_do_equal_len():
388388
""" return True if we have an equal len settable """
389-
if not len(labels) == 1:
389+
if not len(labels) == 1 or not np.iterable(value):
390390
return False
391391

392392
l = len(value)

pandas/tests/test_indexing.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,20 @@ def test_loc_setitem_multiindex(self):
619619
self.assertEqual(result, 0)
620620

621621
df = DataFrame(-999,columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index)
622-
df.loc[(t,n),'X'] = 0
622+
df.loc[(t,n),'X'] = 1
623623
result = df.loc[(t,n),'X']
624-
self.assertEqual(result, 0)
624+
self.assertEqual(result, 1)
625625

626626
df = DataFrame(columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index)
627-
df.loc[(t,n),'X'] = 0
627+
df.loc[(t,n),'X'] = 2
628628
result = df.loc[(t,n),'X']
629-
self.assertEqual(result, 0)
629+
self.assertEqual(result, 2)
630+
631+
# GH 7218, assinging with 0-dim arrays
632+
df = DataFrame(-999,columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index)
633+
df.loc[(t,n), 'X'] = np.array(3)
634+
result = df.loc[(t,n),'X']
635+
self.assertEqual(result,3)
630636

631637
def test_loc_setitem_dups(self):
632638

0 commit comments

Comments
 (0)