From cfb388264dd64c45d6aee1b8724ebc3dd6fcb26d Mon Sep 17 00:00:00 2001 From: Xbar Date: Wed, 16 Mar 2016 19:44:34 -0700 Subject: [PATCH] BUGFIX: fillNA for panel (ndim==3) Issue: 12624 closes #12624 --- doc/source/whatsnew/v0.18.1.txt | 37 ++++----------------------------- pandas/core/generic.py | 16 ++++++++------ pandas/tests/test_panel.py | 18 ++++++++++++++++ 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index b7a0cf888f1a2..88cefae1d4a75 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -89,46 +89,17 @@ Bug Fixes ~~~~~~~~~ - Bug in ``Period`` and ``PeriodIndex`` creation raises ``KeyError`` if ``freq="Minute"`` is specified. Note that "Minute" freq is deprecated in v0.17.0, and recommended to use ``freq="T"`` instead (:issue:`11854`) -- Bug in ``Series`` construction with ``Categorical`` and ``dtype='category'`` is specified (:issue:`12574`) - - - - - - - +- Bug in ``Series`` construction with ``Categorical`` and ``dtype='category'`` is specified (:issue:`12574`) +- BUG: Can't get period code with frequency alias 'minute' or 'Minute' - Bug in ``value_counts`` when ``normalize=True`` and ``dropna=True`` where nulls still contributed to the normalized count (:issue:`12558`) - - - - - - - - - - - - Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`) +- Bug in ``Panel.fillna()`` ignores ``inplace=True`` (:issue:`12633`) - - - - - - - - - - - - - - +- BUG: Crosstab with margins=True ignoring dropna=True - Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 963c953154b57..59d1cf1ca45dc 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3124,13 +3124,17 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, # fill in 2d chunks result = dict([(col, s.fillna(method=method, value=value)) for col, s in self.iteritems()]) - return self._constructor.from_dict(result).__finalize__(self) + new_obj = self._constructor.\ + from_dict(result).__finalize__(self) + new_data = new_obj._data - # 2d or less - method = mis._clean_fill_method(method) - new_data = self._data.interpolate(method=method, axis=axis, - limit=limit, inplace=inplace, - coerce=True, downcast=downcast) + else: + # 2d or less + method = mis._clean_fill_method(method) + new_data = self._data.interpolate(method=method, axis=axis, + limit=limit, inplace=inplace, + coerce=True, + downcast=downcast) else: if method is not None: raise ValueError('cannot specify both a fill method and value') diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 0a1e15921dad7..dbab9a2298282 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1528,6 +1528,24 @@ def test_fillna(self): p.iloc[0:2, 0:2, 0:2] = np.nan self.assertRaises(NotImplementedError, lambda: p.fillna(999, limit=1)) + # Test in place fillNA + # Expected result + expected = Panel([[[0, 1], [2, 1]], [[10, 11], [12, 11]]], + items=['a', 'b'], minor_axis=['x', 'y'], + dtype=np.float64) + # method='ffill' + p1 = Panel([[[0, 1], [2, np.nan]], [[10, 11], [12, np.nan]]], + items=['a', 'b'], minor_axis=['x', 'y'], + dtype=np.float64) + p1.fillna(method='ffill', inplace=True) + assert_panel_equal(p1, expected) + + # method='bfill' + p2 = Panel([[[0, np.nan], [2, 1]], [[10, np.nan], [12, 11]]], + items=['a', 'b'], minor_axis=['x', 'y'], dtype=np.float64) + p2.fillna(method='bfill', inplace=True) + assert_panel_equal(p2, expected) + def test_ffill_bfill(self): assert_panel_equal(self.panel.ffill(), self.panel.fillna(method='ffill'))