Skip to content
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

BUG: fix replace bug when a nested dict was passed #4117

Merged
merged 1 commit into from
Jul 3, 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 doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ pandas 0.12
- Fix bug where ``HDFStore`` will fail to append because of a different block
ordering on-disk (:issue:`4096`)
- Better error messages on inserting incompatible columns to a frame (:issue:`4107`)
- Fixed bug in ``DataFrame.replace`` where a nested dict wasn't being
iterated over when regex=False (:issue:`4115`)


pandas 0.11.0
Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.12.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ Bug Fixes
rewritten in an incompatible way (:issue:`4062`, :issue:`4063`)
- Fixed bug where sharex and sharey were not being passed to grouped_hist
(:issue:`4089`)
- Fixed bug in ``DataFrame.replace`` where a nested dict wasn't being
iterated over when regex=False (:issue:`4115`)

See the :ref:`full release notes
<release>` or issue tracker
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
blk = super(ObjectBlock, self).replace(to_replace, value,
inplace=inplace,
filter=filter, regex=regex)
elif both_lists and regex:
elif both_lists:
for to_rep, v in itertools.izip(to_replace, value):
blk[0], = blk[0]._replace_single(to_rep, v, inplace=inplace,
filter=filter, regex=regex)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6737,6 +6737,11 @@ def test_regex_replace_dict_nested(self):
assert_frame_equal(res3, expec)
assert_frame_equal(res4, expec)

def test_regex_replace_dict_nested_gh4115(self):
df = pd.DataFrame({'Type':['Q','T','Q','Q','T'], 'tmp':2})
expected = DataFrame({'Type': [0,1,0,0,1], 'tmp': 2})
assert_frame_equal(df.replace({'Type': {'Q':0,'T':1}}), expected)

def test_regex_replace_list_to_scalar(self):
mix = {'a': range(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']}
df = DataFrame(mix)
Expand Down