Skip to content

API: deprecate unused DataFrame.replace arguments #3712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down
41 changes: 20 additions & 21 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
33 changes: 5 additions & 28 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down