Skip to content

Commit 1041dff

Browse files
stahlousjreback
authored andcommitted
BUG: setitem fails on mixed-type Panel4D
1 parent 3579304 commit 1041dff

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

doc/source/whatsnew/v0.15.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,5 @@ Bug Fixes
227227
- Fixed a bug where plotting a column ``y`` and specifying a label would mutate the index name of the original DataFrame (:issue:`8494`)
228228

229229
- Bug in ``date_range`` where partially-specified dates would incorporate current date (:issue:`6961`)
230+
231+
- Bug in Setting by indexer to a scalar value with a mixed-dtype `Panel4d` was failing (:issue:`8702`)

pandas/core/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def _setitem_with_indexer(self, indexer, value):
355355
# if we have a partial multiindex, then need to adjust the plane
356356
# indexer here
357357
if (len(labels) == 1 and
358-
isinstance(self.obj[labels[0]].index, MultiIndex)):
358+
isinstance(self.obj[labels[0]].axes[0], MultiIndex)):
359359
item = labels[0]
360360
obj = self.obj[item]
361361
index = obj.index

pandas/tests/test_panel4d.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,53 @@ def test_setitem(self):
372372
self.panel4d['lP'] = self.panel4d['l1'] > 0
373373
self.assertEqual(self.panel4d['lP'].values.dtype, np.bool_)
374374

375+
def test_setitem_by_indexer(self):
376+
377+
# Panel
378+
panel4dc = self.panel4d.copy()
379+
p = panel4dc.iloc[0]
380+
def func():
381+
self.panel4d.iloc[0] = p
382+
self.assertRaises(NotImplementedError, func)
383+
384+
# DataFrame
385+
panel4dc = self.panel4d.copy()
386+
df = panel4dc.iloc[0,0]
387+
df.iloc[:] = 1
388+
panel4dc.iloc[0,0] = df
389+
self.assertTrue((panel4dc.iloc[0,0].values == 1).all())
390+
391+
# Series
392+
panel4dc = self.panel4d.copy()
393+
s = panel4dc.iloc[0,0,:,0]
394+
s.iloc[:] = 1
395+
panel4dc.iloc[0,0,:,0] = s
396+
self.assertTrue((panel4dc.iloc[0,0,:,0].values == 1).all())
397+
398+
# scalar
399+
panel4dc = self.panel4d.copy()
400+
panel4dc.iloc[0] = 1
401+
panel4dc.iloc[1] = True
402+
panel4dc.iloc[2] = 'foo'
403+
self.assertTrue((panel4dc.iloc[0].values == 1).all())
404+
self.assertTrue(panel4dc.iloc[1].values.all())
405+
self.assertTrue((panel4dc.iloc[2].values == 'foo').all())
406+
407+
def test_setitem_by_indexer_mixed_type(self):
408+
# GH 8702
409+
self.panel4d['foo'] = 'bar'
410+
411+
# scalar
412+
panel4dc = self.panel4d.copy()
413+
panel4dc.iloc[0] = 1
414+
panel4dc.iloc[1] = True
415+
panel4dc.iloc[2] = 'foo'
416+
self.assertTrue((panel4dc.iloc[0].values == 1).all())
417+
self.assertTrue(panel4dc.iloc[1].values.all())
418+
self.assertTrue((panel4dc.iloc[2].values == 'foo').all())
419+
420+
421+
375422
def test_comparisons(self):
376423
p1 = tm.makePanel4D()
377424
p2 = tm.makePanel4D()

0 commit comments

Comments
 (0)