Skip to content

BUG: fix replace bug where different dtypes in a nested dict would only replace the first value #6820

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
Apr 8, 2014
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 doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ Bug Fixes
coverted into bools. (:issue:`6806`)
- Regression from 0.13 with ``fillna`` and a Series on datetime-like (:issue:`6344`)
- Bug in adding np.timedelta64 to DatetimeIndex with tz outputs incorrect result (:issue:`6818`)
- Bug in ``DataFrame.replace()`` where changing a dtype through replacement
would only replace the first occurrence of a value (:issue:`6689`)

pandas 0.13.1
-------------
Expand Down
15 changes: 8 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2093,7 +2093,7 @@ def convert_objects(self, convert_dates=True, convert_numeric=False,
strings), non-convertibles get NaN
convert_timedeltas : if True, attempt to soft convert timedeltas, if 'coerce',
force conversion (and non-convertibles get NaT)
copy : Boolean, if True, return copy even if no copy is necessary
copy : Boolean, if True, return copy even if no copy is necessary
(e.g. no conversion was done), default is True.
It is meant for internal use, not to be confused with `inplace` kw.

Expand Down Expand Up @@ -2410,13 +2410,14 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
new_data = self._data
if is_dictlike(to_replace):
if is_dictlike(value): # {'A' : NA} -> {'A' : 0}
res = self if inplace else self.copy()
for c, src in compat.iteritems(to_replace):
if c in value and c in self:
new_data = new_data.replace(to_replace=src,
value=value[c],
filter=[c],
inplace=inplace,
regex=regex)
res[c] = res[c].replace(to_replace=src,
value=value[c],
inplace=False,
regex=regex)
return None if inplace else res

# {'A': NA} -> 0
elif not com.is_list_like(value):
Expand All @@ -2428,7 +2429,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
inplace=inplace,
regex=regex)
else:
raise TypeError('Fill value must be scalar, dict, or '
raise TypeError('value argument must be scalar, dict, or '
'Series')

elif com.is_list_like(to_replace): # [NA, ''] -> [0, 'missing']
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8231,6 +8231,17 @@ def test_replace_str_to_str_chain(self):
with tm.assertRaisesRegexp(ValueError, "Replacement not allowed .+"):
df.replace({'a': dict(zip(astr, bstr))})

def test_replace_swapping_bug(self):
df = pd.DataFrame({'a': [True, False, True]})
res = df.replace({'a': {True: 'Y', False: 'N'}})
expect = pd.DataFrame({'a': ['Y', 'N', 'Y']})
tm.assert_frame_equal(res, expect)

df = pd.DataFrame({'a': [0, 1, 0]})
res = df.replace({'a': {0: 'Y', 1: 'N'}})
expect = pd.DataFrame({'a': ['Y', 'N', 'Y']})
tm.assert_frame_equal(res, expect)

def test_combine_multiple_frames_dtypes(self):

# GH 2759
Expand Down