Skip to content

Commit e529ee8

Browse files
committed
Merge pull request #4117 from cpcloud/replace-nested-dict-fix
BUG: fix replace bug when a nested dict was passed
2 parents 380bd25 + 536f55d commit e529ee8

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ pandas 0.12
314314
- Fix bug where ``HDFStore`` will fail to append because of a different block
315315
ordering on-disk (:issue:`4096`)
316316
- Better error messages on inserting incompatible columns to a frame (:issue:`4107`)
317+
- Fixed bug in ``DataFrame.replace`` where a nested dict wasn't being
318+
iterated over when regex=False (:issue:`4115`)
317319

318320

319321
pandas 0.11.0

doc/source/v0.12.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ Bug Fixes
457457
rewritten in an incompatible way (:issue:`4062`, :issue:`4063`)
458458
- Fixed bug where sharex and sharey were not being passed to grouped_hist
459459
(:issue:`4089`)
460+
- Fixed bug in ``DataFrame.replace`` where a nested dict wasn't being
461+
iterated over when regex=False (:issue:`4115`)
460462

461463
See the :ref:`full release notes
462464
<release>` or issue tracker

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
756756
blk = super(ObjectBlock, self).replace(to_replace, value,
757757
inplace=inplace,
758758
filter=filter, regex=regex)
759-
elif both_lists and regex:
759+
elif both_lists:
760760
for to_rep, v in itertools.izip(to_replace, value):
761761
blk[0], = blk[0]._replace_single(to_rep, v, inplace=inplace,
762762
filter=filter, regex=regex)

pandas/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -6720,6 +6720,11 @@ def test_regex_replace_dict_nested(self):
67206720
assert_frame_equal(res3, expec)
67216721
assert_frame_equal(res4, expec)
67226722

6723+
def test_regex_replace_dict_nested_gh4115(self):
6724+
df = pd.DataFrame({'Type':['Q','T','Q','Q','T'], 'tmp':2})
6725+
expected = DataFrame({'Type': [0,1,0,0,1], 'tmp': 2})
6726+
assert_frame_equal(df.replace({'Type': {'Q':0,'T':1}}), expected)
6727+
67236728
def test_regex_replace_list_to_scalar(self):
67246729
mix = {'a': range(4), 'b': list('ab..'), 'c': ['a', 'b', nan, 'd']}
67256730
df = DataFrame(mix)

0 commit comments

Comments
 (0)