From bd8f31e8903c20587d4a5a6c696bbd14c73ceb65 Mon Sep 17 00:00:00 2001 From: jreback Date: Fri, 23 May 2014 09:50:35 -0400 Subject: [PATCH] BUG: bug in setitem with multi-index and a 0-dim ndarray (GH7218) --- doc/source/release.rst | 2 +- pandas/core/indexing.py | 4 ++-- pandas/tests/test_indexing.py | 14 ++++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 03867e3bf6543..e0ca9854262f1 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -522,7 +522,7 @@ Bug Fixes - Bug in ``query``/``eval`` where global constants were not looked up correctly (:issue:`7178`) - Bug in recognizing out-of-bounds positional list indexers with ``iloc`` and a multi-axis tuple indexer (:issue:`7189`) -- Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`) +- Bug in setitem with a single value, multi-index and integer indices (:issue:`7190`, :issue:`7218`) - Bug in expressions evaluation with reversed ops, showing in series-dataframe ops (:issue:`7198`, :issue:`7192`) - Bug in multi-axis indexing with > 2 ndim and a multi-index (:issue:`7199`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index df4e5cb738899..518879105aa8b 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -339,7 +339,7 @@ def _setitem_with_indexer(self, indexer, value): # require that we are setting the right number of values that # we are indexing - if is_list_like(value) and lplane_indexer != len(value): + if is_list_like(value) and np.iterable(value) and lplane_indexer != len(value): if len(obj[idx]) != len(value): raise ValueError( @@ -386,7 +386,7 @@ def setter(item, v): def can_do_equal_len(): """ return True if we have an equal len settable """ - if not len(labels) == 1: + if not len(labels) == 1 or not np.iterable(value): return False l = len(value) diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index f5bae7b2d2c82..14f2ee6222238 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -619,14 +619,20 @@ def test_loc_setitem_multiindex(self): self.assertEqual(result, 0) df = DataFrame(-999,columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index) - df.loc[(t,n),'X'] = 0 + df.loc[(t,n),'X'] = 1 result = df.loc[(t,n),'X'] - self.assertEqual(result, 0) + self.assertEqual(result, 1) df = DataFrame(columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index) - df.loc[(t,n),'X'] = 0 + df.loc[(t,n),'X'] = 2 result = df.loc[(t,n),'X'] - self.assertEqual(result, 0) + self.assertEqual(result, 2) + + # GH 7218, assinging with 0-dim arrays + df = DataFrame(-999,columns=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'], index=index) + df.loc[(t,n), 'X'] = np.array(3) + result = df.loc[(t,n),'X'] + self.assertEqual(result,3) def test_loc_setitem_dups(self):