From e7115a540688de8e61a8459309fd3995ba1a652e Mon Sep 17 00:00:00 2001 From: Nick Stahl Date: Sat, 1 Nov 2014 13:38:24 -0400 Subject: [PATCH] BUG: setitem fails on mixed-type Panel4D --- doc/source/whatsnew/v0.15.1.txt | 2 ++ pandas/core/indexing.py | 2 +- pandas/tests/test_panel4d.py | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.15.1.txt b/doc/source/whatsnew/v0.15.1.txt index e96adc2bd9559..96dd8e2fe8e59 100644 --- a/doc/source/whatsnew/v0.15.1.txt +++ b/doc/source/whatsnew/v0.15.1.txt @@ -227,3 +227,5 @@ Bug Fixes - Fixed a bug where plotting a column ``y`` and specifying a label would mutate the index name of the original DataFrame (:issue:`8494`) - Bug in ``date_range`` where partially-specified dates would incorporate current date (:issue:`6961`) + +- Setting by indexer to a scalar value with a mixed-dtype `Panel4d` was failing (:issue: `8702`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 954acb0f95159..048e4af20d02f 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -355,7 +355,7 @@ def _setitem_with_indexer(self, indexer, value): # if we have a partial multiindex, then need to adjust the plane # indexer here if (len(labels) == 1 and - isinstance(self.obj[labels[0]].index, MultiIndex)): + isinstance(self.obj[labels[0]].axes[0], MultiIndex)): item = labels[0] obj = self.obj[item] index = obj.index diff --git a/pandas/tests/test_panel4d.py b/pandas/tests/test_panel4d.py index e88a8c3b2874c..6ef8c1820400a 100644 --- a/pandas/tests/test_panel4d.py +++ b/pandas/tests/test_panel4d.py @@ -372,6 +372,53 @@ def test_setitem(self): self.panel4d['lP'] = self.panel4d['l1'] > 0 self.assertEqual(self.panel4d['lP'].values.dtype, np.bool_) + def test_setitem_by_indexer(self): + + # Panel + panel4dc = self.panel4d.copy() + p = panel4dc.iloc[0] + def func(): + self.panel4d.iloc[0] = p + self.assertRaises(NotImplementedError, func) + + # DataFrame + panel4dc = self.panel4d.copy() + df = panel4dc.iloc[0,0] + df.iloc[:] = 1 + panel4dc.iloc[0,0] = df + self.assertTrue((panel4dc.iloc[0,0].values == 1).all()) + + # Series + panel4dc = self.panel4d.copy() + s = panel4dc.iloc[0,0,:,0] + s.iloc[:] = 1 + panel4dc.iloc[0,0,:,0] = s + self.assertTrue((panel4dc.iloc[0,0,:,0].values == 1).all()) + + # scalar + panel4dc = self.panel4d.copy() + panel4dc.iloc[0] = 1 + panel4dc.iloc[1] = True + panel4dc.iloc[2] = 'foo' + self.assertTrue((panel4dc.iloc[0].values == 1).all()) + self.assertTrue(panel4dc.iloc[1].values.all()) + self.assertTrue((panel4dc.iloc[2].values == 'foo').all()) + + def test_setitem_by_indexer_mixed_type(self): + # GH 8702 + self.panel4d['foo'] = 'bar' + + # scalar + panel4dc = self.panel4d.copy() + panel4dc.iloc[0] = 1 + panel4dc.iloc[1] = True + panel4dc.iloc[2] = 'foo' + self.assertTrue((panel4dc.iloc[0].values == 1).all()) + self.assertTrue(panel4dc.iloc[1].values.all()) + self.assertTrue((panel4dc.iloc[2].values == 'foo').all()) + + + def test_comparisons(self): p1 = tm.makePanel4D() p2 = tm.makePanel4D()