Skip to content

Commit 77d9c92

Browse files
committed
Merge pull request #11021 from evanpw/panel_set_minor_major
BUG: Exception when setting a major- or minor-axis slice of a Panel with RHS a DataFrame
2 parents 81b647f + a80808f commit 77d9c92

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ Bug Fixes
964964

965965
- Bug causes memory leak in time-series line and area plot (:issue:`9003`)
966966

967+
- Bug when setting a ``Panel`` sliced along the major or minor axes when the right-hand side is a ``DataFrame`` (:issue:`11014`)
968+
967969

968970
- Bug in line and kde plot cannot accept multiple colors when ``subplots=True`` (:issue:`9894`)
969971
- Bug in ``DataFrame.plot`` raises ``ValueError`` when color name is specified by multiple characters (:issue:`10387`)

pandas/core/indexing.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,15 @@ def can_do_equal_len():
442442

443443
# we have an equal len Frame
444444
if isinstance(value, ABCDataFrame) and value.ndim > 1:
445+
sub_indexer = list(indexer)
445446

446447
for item in labels:
447-
# align to
448-
v = np.nan if item not in value else \
449-
self._align_series(indexer[0], value[item])
448+
if item in value:
449+
sub_indexer[info_axis] = item
450+
v = self._align_series(tuple(sub_indexer), value[item])
451+
else:
452+
v = np.nan
453+
450454
setter(item, v)
451455

452456
# we have an equal len ndarray/convertible to our labels

pandas/tests/test_panel.py

+14
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,20 @@ def test_setitem_ndarray(self):
506506

507507
assert_almost_equal(P[key].values, data)
508508

509+
def test_set_minor_major(self):
510+
# GH 11014
511+
df1 = DataFrame(['a', 'a', 'a', np.nan, 'a', np.nan])
512+
df2 = DataFrame([1.0, np.nan, 1.0, np.nan, 1.0, 1.0])
513+
panel = Panel({'Item1' : df1, 'Item2': df2})
514+
515+
newminor = notnull(panel.iloc[:, :, 0])
516+
panel.loc[:, :, 'NewMinor'] = newminor
517+
assert_frame_equal(panel.loc[:, :, 'NewMinor'], newminor.astype(object))
518+
519+
newmajor = notnull(panel.iloc[:, 0, :])
520+
panel.loc[:, 'NewMajor', :] = newmajor
521+
assert_frame_equal(panel.loc[:, 'NewMajor', :], newmajor.astype(object))
522+
509523
def test_major_xs(self):
510524
ref = self.panel['ItemA']
511525

0 commit comments

Comments
 (0)