Skip to content

BUG: setitem fails on mixed-type Panel4D #8702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions pandas/tests/test_panel4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment in the tests (or PR number if no issue)

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()
Expand Down