Skip to content

BUG: Handle Series arguments in DataFrame.replace, fixes GH2994 #3064

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3427,8 +3427,8 @@ def replace(self, to_replace, value=None, method='pad', axis=0,

Parameters
----------
value : scalar or dict, default None
Value to use to fill holes (e.g. 0), alternately a dict of values
value : scalar or dict or Series, default None
Value to use to fill holes (e.g. 0), alternately a dict/Series of values
specifying which value to use for each column (columns not in the
dict will not be filled)
method : {'backfill', 'bfill', 'pad', 'ffill', None}, default 'pad'
Expand Down Expand Up @@ -3468,8 +3468,8 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
return self

new_data = self._data
if isinstance(to_replace, dict):
if isinstance(value, dict): # {'A' : NA} -> {'A' : 0}
if isinstance(to_replace, (dict,Series)):
if isinstance(value, (dict,Series)): # {'A' : NA} -> {'A' : 0}
new_data = self._data
for c, src in to_replace.iteritems():
if c in value and c in self:
Expand Down Expand Up @@ -3501,7 +3501,7 @@ def replace(self, to_replace, value=None, method='pad', axis=0,
else:

# dest iterable dict-like
if isinstance(value, dict): # NA -> {'A' : 0, 'B' : -1}
if isinstance(value, (dict,Series)): # NA -> {'A' : 0, 'B' : -1}

new_data = self._data
for k, v in value.iteritems():
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5786,6 +5786,23 @@ def test_replace_input_formats(self):
expected.replace(to_rep[i], -1, inplace=True)
assert_frame_equal(result, expected)

# scalar to series/dict
df = DataFrame({'zero': {'a': 0.0, 'b': 1}, 'one': {'a': 2.0, 'b': 0}})
result = df.replace(0, {'zero': 0.5, 'one': 1.0})
expected = DataFrame({'zero': {'a': 0.5, 'b': 1}, 'one': {'a': 2.0, 'b': 1.0}})
assert_frame_equal(result, expected)
result = df.replace(0, df.mean())
assert_frame_equal(result, expected)

# series to series/dict
df = DataFrame({'zero': {'a': 0.0, 'b': 1}, 'one': {'a': 2.0, 'b': 0}})
s = Series({'zero': 0.0, 'one': 2.0})
result = df.replace(s, {'zero': 0.5, 'one': 1.0})
expected = DataFrame({'zero': {'a': 0.5, 'b': 1}, 'one': {'a': 1.0, 'b': 0.0}})
assert_frame_equal(result, expected)
result = df.replace(s, df.mean())
assert_frame_equal(result, expected)

def test_replace_axis(self):
self.tsframe['A'][:5] = nan
self.tsframe['A'][-5:] = nan
Expand Down