Skip to content

Commit 7192496

Browse files
committed
API: deprecate unused DataFrame.replace arguments
1 parent 7413da1 commit 7192496

File tree

4 files changed

+29
-49
lines changed

4 files changed

+29
-49
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pandas 0.11.1
110110
- added ``pandas.io.api`` for i/o imports
111111
- removed ``Excel`` support to ``pandas.io.excel``
112112
- added top-level ``pd.read_sql`` and ``to_sql`` DataFrame methods
113+
- the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are
114+
deprecated
113115

114116
**Bug Fixes**
115117

doc/source/v0.11.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ API changes
8383
- ``DataFrame.interpolate()`` is now deprecated. Please use
8484
``DataFrame.fillna()`` and ``DataFrame.replace()`` instead. (GH3582_,
8585
GH3675_, GH3676_)
86+
- the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are
87+
deprecated
8688

8789
- Add the keyword ``allow_duplicates`` to ``DataFrame.insert`` to allow a duplicate column
8890
to be inserted if ``True``, default is ``False`` (same as prior to 0.11.1) (GH3679_)

pandas/core/frame.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,9 +3481,9 @@ def bfill(self, axis=0, inplace=False, limit=None):
34813481
return self.fillna(method='bfill', axis=axis, inplace=inplace,
34823482
limit=limit)
34833483

3484-
def replace(self, to_replace=None, value=None, method='pad', axis=0,
3485-
inplace=False, limit=None, regex=False, infer_types=False):
3486-
"""Replace values given in 'to_replace' with 'value' or using 'method'.
3484+
def replace(self, to_replace=None, value=None, inplace=False, limit=None,
3485+
regex=False, infer_types=False, method=None, axis=None):
3486+
"""Replace values given in 'to_replace' with 'value'.
34873487
34883488
Parameters
34893489
----------
@@ -3521,13 +3521,6 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
35213521
specifying which value to use for each column (columns not in the
35223522
dict will not be filled). Regular expressions, strings and lists or
35233523
dicts of such objects are also allowed.
3524-
method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad'
3525-
Method to use for filling holes in reindexed Series
3526-
pad / ffill: propagate last valid observation forward to next valid
3527-
backfill / bfill: use NEXT valid observation to fill gap
3528-
axis : {0, 1}, default 0
3529-
0: fill column-by-column
3530-
1: fill row-by-row
35313524
inplace : boolean, default False
35323525
If True, fill the DataFrame in place. Note: this will modify any
35333526
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,
35803573
if not isinstance(regex, bool) and to_replace is not None:
35813574
raise AssertionError("'to_replace' must be 'None' if 'regex' is "
35823575
"not a bool")
3583-
self._consolidate_inplace()
3576+
if method is not None:
3577+
from warnings import warn
3578+
warn('the "method" argument is deprecated and will be removed in'
3579+
'v0.12; this argument has no effect')
35843580

3585-
axis = self._get_axis_number(axis)
3586-
method = com._clean_fill_method(method)
3581+
if axis is not None:
3582+
from warnings import warn
3583+
warn('the "axis" argument is deprecated and will be removed in'
3584+
'v0.12; this argument has no effect')
3585+
3586+
self._consolidate_inplace()
35873587

35883588
if value is None:
35893589
if not isinstance(to_replace, (dict, Series)):
@@ -3615,8 +3615,8 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
36153615
else:
36163616
to_replace, value = keys, values
36173617

3618-
return self.replace(to_replace, value, method=method, axis=axis,
3619-
inplace=inplace, limit=limit, regex=regex,
3618+
return self.replace(to_replace, value, inplace=inplace,
3619+
limit=limit, regex=regex,
36203620
infer_types=infer_types)
36213621
else:
36223622
if not len(self.columns):
@@ -3629,7 +3629,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
36293629
for c, src in to_replace.iteritems():
36303630
if c in value and c in self:
36313631
new_data = new_data.replace(src, value[c],
3632-
filter=[ c ],
3632+
filter=[c],
36333633
inplace=inplace,
36343634
regex=regex)
36353635

@@ -3638,7 +3638,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
36383638
for k, src in to_replace.iteritems():
36393639
if k in self:
36403640
new_data = new_data.replace(src, value,
3641-
filter = [ k ],
3641+
filter=[k],
36423642
inplace=inplace,
36433643
regex=regex)
36443644
else:
@@ -3667,9 +3667,8 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
36673667
"regular expression or a list or dict of "
36683668
"strings or regular expressions, you "
36693669
"passed a {0}".format(type(regex)))
3670-
return self.replace(regex, value, method=method, axis=axis,
3671-
inplace=inplace, limit=limit, regex=True,
3672-
infer_types=infer_types)
3670+
return self.replace(regex, value, inplace=inplace, limit=limit,
3671+
regex=True, infer_types=infer_types)
36733672
else:
36743673

36753674
# dest iterable dict-like
@@ -3679,7 +3678,7 @@ def replace(self, to_replace=None, value=None, method='pad', axis=0,
36793678
for k, v in value.iteritems():
36803679
if k in self:
36813680
new_data = new_data.replace(to_replace, v,
3682-
filter=[ k ],
3681+
filter=[k],
36833682
inplace=inplace,
36843683
regex=regex)
36853684

pandas/tests/test_frame.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6360,8 +6360,7 @@ def test_replace_inplace(self):
63606360
res = tsframe.replace(nan, 0, inplace=True)
63616361
assert_frame_equal(tsframe, self.tsframe.fillna(0))
63626362

6363-
self.assertRaises(TypeError, self.tsframe.replace, nan, method='pad',
6364-
inplace=True)
6363+
self.assertRaises(TypeError, self.tsframe.replace, nan, inplace=True)
63656364

63666365
# mixed type
63676366
self.mixed_frame['foo'][5:20] = nan
@@ -6953,21 +6952,18 @@ def test_interpolate(self):
69536952
pass
69546953

69556954
def test_replace_value_is_none(self):
6956-
self.assertRaises(TypeError, self.tsframe.replace, nan, method='pad')
6955+
self.assertRaises(TypeError, self.tsframe.replace, nan)
69576956
orig_value = self.tsframe.iloc[0, 0]
69586957
orig2 = self.tsframe.iloc[1, 0]
69596958

69606959
self.tsframe.iloc[0, 0] = nan
69616960
self.tsframe.iloc[1, 0] = 1
69626961

6963-
result = self.tsframe.replace(to_replace={nan: 0}, method='pad',
6964-
axis=1)
6965-
expected = self.tsframe.T.replace(
6966-
to_replace={nan: 0}, method='pad').T
6962+
result = self.tsframe.replace(to_replace={nan: 0})
6963+
expected = self.tsframe.T.replace(to_replace={nan: 0}).T
69676964
assert_frame_equal(result, expected)
69686965

6969-
result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8},
6970-
method='bfill')
6966+
result = self.tsframe.replace(to_replace={nan: 0, 1: -1e8})
69716967
tsframe = self.tsframe.copy()
69726968
tsframe.iloc[0, 0] = 0
69736969
tsframe.iloc[1, 0] = -1e8
@@ -7088,25 +7084,6 @@ def test_replace_input_formats(self):
70887084
expected.replace(to_rep[i], -1, inplace=True)
70897085
assert_frame_equal(result, expected)
70907086

7091-
def test_replace_axis(self):
7092-
self.tsframe['A'][:5] = nan
7093-
self.tsframe['A'][-5:] = nan
7094-
7095-
zero_filled = self.tsframe.replace(nan, 0, axis=1)
7096-
assert_frame_equal(zero_filled, self.tsframe.fillna(0, axis=1))
7097-
7098-
self.assertRaises(TypeError, self.tsframe.replace, method='pad',
7099-
axis=1)
7100-
7101-
# mixed type
7102-
self.mixed_frame['foo'][5:20] = nan
7103-
self.mixed_frame['A'][-10:] = nan
7104-
7105-
result = self.mixed_frame.replace(np.nan, -1e8, axis=1)
7106-
expected = self.mixed_frame.fillna(value=-1e8, axis=1)
7107-
assert_frame_equal(result, expected)
7108-
7109-
71107087
def test_replace_limit(self):
71117088
pass
71127089

0 commit comments

Comments
 (0)