Skip to content

BUG: fix metacharacter replacement bug in DataFrame.replace() #6778

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 4, 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
3 changes: 2 additions & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ Bug Fixes
- Bug in downcasting inference with empty arrays (:issue:`6733`)
- Bug in ``obj.blocks`` on sparse containers dropping all but the last items of same for dtype (:issue:`6748`)
- Bug in unpickling ``NaT (NaTType)`` (:issue:`4606`)
- Bug in setting a tz-aware index directly via ``.index`` (:issue:`6785`)
- Bug in ``DataFrame.replace()`` where regex metacharacters were being treated
as regexs even when ``regex=False`` (:issue:`6777`).

pandas 0.13.1
-------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
def _replace_single(self, to_replace, value, inplace=False, filter=None,
regex=False):
# to_replace is regex compilable
to_rep_re = com.is_re_compilable(to_replace)
to_rep_re = regex and com.is_re_compilable(to_replace)

# regex is regex compilable
regex_re = com.is_re_compilable(regex)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7857,6 +7857,15 @@ def test_regex_replace_numeric_to_object_conversion(self):
assert_frame_equal(res, expec)
self.assertEqual(res.a.dtype, np.object_)

def test_replace_regex_metachar(self):
metachars = '[]', '()', '\d', '\w', '\s'

for metachar in metachars:
df = DataFrame({'a': [metachar, 'else']})
result = df.replace({'a': {metachar: 'paren'}})
expected = DataFrame({'a': ['paren', 'else']})
tm.assert_frame_equal(result, expected)

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