diff --git a/RELEASE.rst b/RELEASE.rst index 1d63a7f53954d..e611b330b08f0 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -110,6 +110,8 @@ pandas 0.11.1 - added ``pandas.io.api`` for i/o imports - removed ``Excel`` support to ``pandas.io.excel`` - added top-level ``pd.read_sql`` and ``to_sql`` DataFrame methods + - the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are + deprecated **Bug Fixes** diff --git a/doc/source/v0.11.1.txt b/doc/source/v0.11.1.txt index 9113c74c6813b..c025450c44cca 100644 --- a/doc/source/v0.11.1.txt +++ b/doc/source/v0.11.1.txt @@ -83,6 +83,8 @@ API changes - ``DataFrame.interpolate()`` is now deprecated. Please use ``DataFrame.fillna()`` and ``DataFrame.replace()`` instead. (GH3582_, GH3675_, GH3676_) + - the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are + deprecated - Add the keyword ``allow_duplicates`` to ``DataFrame.insert`` to allow a duplicate column to be inserted if ``True``, default is ``False`` (same as prior to 0.11.1) (GH3679_) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3ad8de077f1ea..1dfeae997451a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3481,9 +3481,9 @@ def bfill(self, axis=0, inplace=False, limit=None): return self.fillna(method='bfill', axis=axis, inplace=inplace, limit=limit) - def replace(self, to_replace=None, value=None, method='pad', axis=0, - inplace=False, limit=None, regex=False, infer_types=False): - """Replace values given in 'to_replace' with 'value' or using 'method'. + def replace(self, to_replace=None, value=None, inplace=False, limit=None, + regex=False, infer_types=False, method=None, axis=None): + """Replace values given in 'to_replace' with 'value'. Parameters ---------- @@ -3521,13 +3521,6 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, specifying which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. - method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad' - Method to use for filling holes in reindexed Series - pad / ffill: propagate last valid observation forward to next valid - backfill / bfill: use NEXT valid observation to fill gap - axis : {0, 1}, default 0 - 0: fill column-by-column - 1: fill row-by-row inplace : boolean, default False If True, fill the DataFrame in place. Note: this will modify any other views on this DataFrame, like if you took a no-copy slice of @@ -3580,10 +3573,17 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, if not isinstance(regex, bool) and to_replace is not None: raise AssertionError("'to_replace' must be 'None' if 'regex' is " "not a bool") - self._consolidate_inplace() + if method is not None: + from warnings import warn + warn('the "method" argument is deprecated and will be removed in' + 'v0.12; this argument has no effect') - axis = self._get_axis_number(axis) - method = com._clean_fill_method(method) + if axis is not None: + from warnings import warn + warn('the "axis" argument is deprecated and will be removed in' + 'v0.12; this argument has no effect') + + self._consolidate_inplace() if value is None: if not isinstance(to_replace, (dict, Series)): @@ -3615,8 +3615,8 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, else: to_replace, value = keys, values - return self.replace(to_replace, value, method=method, axis=axis, - inplace=inplace, limit=limit, regex=regex, + return self.replace(to_replace, value, inplace=inplace, + limit=limit, regex=regex, infer_types=infer_types) else: if not len(self.columns): @@ -3629,7 +3629,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, for c, src in to_replace.iteritems(): if c in value and c in self: new_data = new_data.replace(src, value[c], - filter=[ c ], + filter=[c], inplace=inplace, regex=regex) @@ -3638,7 +3638,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, for k, src in to_replace.iteritems(): if k in self: new_data = new_data.replace(src, value, - filter = [ k ], + filter=[k], inplace=inplace, regex=regex) else: @@ -3667,9 +3667,8 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, "regular expression or a list or dict of " "strings or regular expressions, you " "passed a {0}".format(type(regex))) - return self.replace(regex, value, method=method, axis=axis, - inplace=inplace, limit=limit, regex=True, - infer_types=infer_types) + return self.replace(regex, value, inplace=inplace, limit=limit, + regex=True, infer_types=infer_types) else: # dest iterable dict-like @@ -3679,7 +3678,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0, for k, v in value.iteritems(): if k in self: new_data = new_data.replace(to_replace, v, - filter=[ k ], + filter=[k], inplace=inplace, regex=regex) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 4e892f884e541..1de643985d893 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6360,8 +6360,7 @@ def test_replace_inplace(self): res = tsframe.replace(nan, 0, inplace=True) assert_frame_equal(tsframe, self.tsframe.fillna(0)) - self.assertRaises(TypeError, self.tsframe.replace, nan, method='pad', - inplace=True) + self.assertRaises(TypeError, self.tsframe.replace, nan, inplace=True) # mixed type self.mixed_frame['foo'][5:20] = nan @@ -6953,21 +6952,18 @@ def test_interpolate(self): pass def test_replace_value_is_none(self): - self.assertRaises(TypeError, self.tsframe.replace, nan, method='pad') + self.assertRaises(TypeError, self.tsframe.replace, nan) orig_value = self.tsframe.iloc[0, 0] orig2 = self.tsframe.iloc[1, 0] self.tsframe.iloc[0, 0] = nan self.tsframe.iloc[1, 0] = 1 - result = self.tsframe.replace(to_replace={nan: 0}, method='pad', - axis=1) - expected = self.tsframe.T.replace( - to_replace={nan: 0}, method='pad').T + result = self.tsframe.replace(to_replace={nan: 0}) + expected = self.tsframe.T.replace(to_replace={nan: 0}).T assert_frame_equal(result, expected) - result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8}, - method='bfill') + result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8}) tsframe = self.tsframe.copy() tsframe.iloc[0, 0] = 0 tsframe.iloc[1, 0] = -1e8 @@ -7088,25 +7084,6 @@ def test_replace_input_formats(self): expected.replace(to_rep[i], -1, inplace=True) assert_frame_equal(result, expected) - def test_replace_axis(self): - self.tsframe['A'][:5] = nan - self.tsframe['A'][-5:] = nan - - zero_filled = self.tsframe.replace(nan, 0, axis=1) - assert_frame_equal(zero_filled, self.tsframe.fillna(0, axis=1)) - - self.assertRaises(TypeError, self.tsframe.replace, method='pad', - axis=1) - - # mixed type - self.mixed_frame['foo'][5:20] = nan - self.mixed_frame['A'][-10:] = nan - - result = self.mixed_frame.replace(np.nan, -1e8, axis=1) - expected = self.mixed_frame.fillna(value=-1e8, axis=1) - assert_frame_equal(result, expected) - - def test_replace_limit(self): pass