From 536f55dbee1a21778838c2435ab0e78d7e48d44d Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Wed, 3 Jul 2013 12:06:52 -0400 Subject: [PATCH] BUG: fix replace bug when a nested dict was passed --- doc/source/release.rst | 2 ++ doc/source/v0.12.0.txt | 2 ++ pandas/core/internals.py | 2 +- pandas/tests/test_frame.py | 5 +++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index d410f39da3ab9..a7469ba2e707b 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 diff --git a/doc/source/v0.12.0.txt b/doc/source/v0.12.0.txt index c68551123cac2..b44d54d0eb31e 100644 --- a/doc/source/v0.12.0.txt +++ b/doc/source/v0.12.0.txt @@ -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 ` or issue tracker diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 57be20a50f7bc..99af2d7becb39 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -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) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index a8a435e3bb660..71892575002f2 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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)