diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index e58bb6f703b3d..e202bea960e12 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -142,7 +142,7 @@ Bug Fixes - +- Bug in using grouper functions that need passed thru arguments (e.g. axis), when using wrapped function (e.g. ``fillna``), (:issue:`9221`) - DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`) - Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index dd48a470e7dd4..7fa64e0b4ca91 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2252,7 +2252,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False, #---------------------------------------------------------------------- # Filling NA's - def fillna(self, value=None, method=None, axis=0, inplace=False, + def fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None): """ Fill NA/NaN values using the specified method @@ -2295,6 +2295,10 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, 'you passed a "{0}"'.format(type(value).__name__)) self._consolidate_inplace() + # 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 axis = self._get_axis_number(axis) method = com._clean_fill_method(method) @@ -2383,12 +2387,12 @@ def fillna(self, value=None, method=None, axis=0, inplace=False, else: return self._constructor(new_data).__finalize__(self) - def ffill(self, axis=0, inplace=False, limit=None, downcast=None): + def ffill(self, axis=None, inplace=False, limit=None, downcast=None): "Synonym for NDFrame.fillna(method='ffill')" return self.fillna(method='ffill', axis=axis, inplace=inplace, limit=limit, downcast=downcast) - def bfill(self, axis=0, inplace=False, limit=None, downcast=None): + def bfill(self, axis=None, inplace=False, limit=None, downcast=None): "Synonym for NDFrame.fillna(method='bfill')" return self.fillna(method='bfill', axis=axis, inplace=inplace, limit=limit, downcast=downcast) diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 039bb2cd599e5..4077f468d8b1f 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -4322,6 +4322,21 @@ def test_filter_non_bool_raises(self): with tm.assertRaisesRegexp(TypeError, 'filter function returned a.*'): df.groupby('a').filter(lambda g: g.c.mean()) + def test_fill_constistency(self): + + # GH9221 + # pass thru keyword arguments to the generated wrapper + # are set if the passed kw is None (only) + df = DataFrame(index=pd.MultiIndex.from_product([['value1','value2'], + date_range('2014-01-01','2014-01-06')]), + columns=Index(['1','2'], name='id')) + df['1'] = [np.nan, 1, np.nan, np.nan, 11, np.nan, np.nan, 2, np.nan, np.nan, 22, np.nan] + df['2'] = [np.nan, 3, np.nan, np.nan, 33, np.nan, np.nan, 4, np.nan, np.nan, 44, np.nan] + + expected = df.groupby(level=0, axis=0).fillna(method='ffill') + result = df.T.groupby(level=0, axis=1).fillna(method='ffill').T + assert_frame_equal(result, expected) + def test_index_label_overlaps_location(self): # checking we don't have any label/location confusion in the # the wake of GH5375