From 305858861b46d937fe0a93169a64105f623d46d5 Mon Sep 17 00:00:00 2001 From: Tux1 Date: Sat, 29 Oct 2016 00:05:05 +0800 Subject: [PATCH] GH #14499 Panel.ffill ignores axis parameter and fill along axis=1 --- doc/source/whatsnew/v0.19.1.txt | 4 +++- pandas/core/generic.py | 13 ++++++++++--- pandas/tests/test_panel.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index a81ab6ed0311c1..f1e0c4c88c558b 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -80,4 +80,6 @@ Bug Fixes - Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` - is not scalar and ``values`` is not specified (:issue:`14380`) \ No newline at end of file + is not scalar and ``values`` is not specified (:issue:`14380`) + +- Bug in ``pd.Panel.ffill`` where ``axis`` was not propagated (:issue:`14499`) \ No newline at end of file diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 037ab900e6150e..9d7098b3aa01b9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3208,7 +3208,10 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, # set the default here, so functions examining the signaure # can detect if something was set (e.g. in groupby) (GH9221) if axis is None: - axis = 0 + if self.ndim == 3: + axis = 1 + else: + axis = 0 axis = self._get_axis_number(axis) method = missing.clean_fill_method(method) @@ -3233,9 +3236,13 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, # 3d elif self.ndim == 3: - + if axis == 0: + raise NotImplementedError('Cannot fillna with axis=0 ' + 'for Panel') # fill in 2d chunks - result = dict([(col, s.fillna(method=method, value=value)) + result = dict([(col, s.fillna(method=method, + value=value, + axis=axis-1)) for col, s in self.iteritems()]) new_obj = self._constructor.\ from_dict(result).__finalize__(self) diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index a197037789fd2a..4669c6b4a6a38d 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1519,6 +1519,36 @@ def test_ffill_bfill(self): assert_panel_equal(self.panel.bfill(), self.panel.fillna(method='bfill')) + def test_ffill_bfill_axis(self): + #GH 14499 + a = Panel({ + 'a':np.arange(15).reshape((5, 3)), + 'b':np.arange(15, 30).reshape((5, 3)) + }) + f1 = a.copy() + f2 = a.copy() + b1 = a.copy() + b2 = a.copy() + a['a'].loc[1, 1] = np.nan + f1['a'].loc[1, 1] = 1 + f2['a'].loc[1, 1] = 3 + + # method='ffill' + # axis=1 + assert_panel_equal(a.ffill(axis=1), f1) + + # method='ffill' + # axis=2 + assert_panel_equal(a.ffill(axis=2), f2) + + # method='bfill' + # axis=1 + assert_panel_equal(a.bfill(axis=1), b1) + + # method='bfill' + # axis=2 + assert_panel_equal(a.bfill(axis=2), b2) + def test_truncate_fillna_bug(self): # #1823 result = self.panel.truncate(before=None, after=None, axis='items')