From a80808f15a6c25db53a0a0192759044dcc7f610a Mon Sep 17 00:00:00 2001 From: Evan Wright Date: Mon, 7 Sep 2015 14:01:28 -0400 Subject: [PATCH] BUG: Exception when setting a major- or minor-axis slice of a Panel with RHS a DataFrame (GH 11014) --- doc/source/whatsnew/v0.17.0.txt | 2 ++ pandas/core/indexing.py | 10 +++++++--- pandas/tests/test_panel.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index f88e5c0a11f9f..4e8cfdfa16a12 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -964,6 +964,8 @@ Bug Fixes - Bug causes memory leak in time-series line and area plot (:issue:`9003`) +- Bug when setting a ``Panel`` sliced along the major or minor axes when the right-hand side is a ``DataFrame`` (:issue:`11014`) + - Bug in line and kde plot cannot accept multiple colors when ``subplots=True`` (:issue:`9894`) - Bug in ``DataFrame.plot`` raises ``ValueError`` when color name is specified by multiple characters (:issue:`10387`) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 7cf1942046e75..71bba3a9edea2 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -442,11 +442,15 @@ def can_do_equal_len(): # we have an equal len Frame if isinstance(value, ABCDataFrame) and value.ndim > 1: + sub_indexer = list(indexer) for item in labels: - # align to - v = np.nan if item not in value else \ - self._align_series(indexer[0], value[item]) + if item in value: + sub_indexer[info_axis] = item + v = self._align_series(tuple(sub_indexer), value[item]) + else: + v = np.nan + setter(item, v) # we have an equal len ndarray/convertible to our labels diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index d45e6e50cafb3..7c67ded16139c 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -506,6 +506,20 @@ def test_setitem_ndarray(self): assert_almost_equal(P[key].values, data) + def test_set_minor_major(self): + # GH 11014 + df1 = DataFrame(['a', 'a', 'a', np.nan, 'a', np.nan]) + df2 = DataFrame([1.0, np.nan, 1.0, np.nan, 1.0, 1.0]) + panel = Panel({'Item1' : df1, 'Item2': df2}) + + newminor = notnull(panel.iloc[:, :, 0]) + panel.loc[:, :, 'NewMinor'] = newminor + assert_frame_equal(panel.loc[:, :, 'NewMinor'], newminor.astype(object)) + + newmajor = notnull(panel.iloc[:, 0, :]) + panel.loc[:, 'NewMajor', :] = newmajor + assert_frame_equal(panel.loc[:, 'NewMajor', :], newmajor.astype(object)) + def test_major_xs(self): ref = self.panel['ItemA']