Skip to content

single example for release notes #3639

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
May 19, 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
6 changes: 3 additions & 3 deletions doc/source/missing_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ or you can pass the nested dictionary like so

.. ipython:: python

df.replace(regex={'b': {'b': r'\s*\.\s*'}})
df.replace(regex={'b': {r'\s*\.\s*': nan}})

You can also use the group of a regular expression match when replacing (dict
of regex -> dict of regex), this works for lists as well
Expand All @@ -420,7 +420,7 @@ will be replaced with a scalar (list of regex -> regex)

.. ipython:: python

df.replace([r'\s*\.\*', r'a|b'], nan, regex=True)
df.replace([r'\s*\.\s*', r'a|b'], nan, regex=True)

All of the regular expression examples can also be passed with the
``to_replace`` argument as the ``regex`` argument. In this case the ``value``
Expand All @@ -429,7 +429,7 @@ dictionary. The previous example, in this case, would then be

.. ipython:: python

df.replace(regex=[r'\s*\.\*', r'a|b'], value=nan)
df.replace(regex=[r'\s*\.\s*', r'a|b'], value=nan)

This can be convenient if you do not want to pass ``regex=True`` every time you
want to use a regular expression.
Expand Down
18 changes: 18 additions & 0 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ Enhancements
``Series`` with object dtype. See the examples section in the regular docs
:ref:`Replacing via String Expression <missing_data.replace_expression>`

For example you can do

.. ipython :: python

df = DataFrame({'a': list('ab..'), 'b': [1, 2, 3, 4]})
df.replace(regex=r'\s*\.\s*', value=nan)

to replace all occurrences of the string ``'.'`` with zero or more
instances of surrounding whitespace with ``NaN``.

Regular string replacement still works as expected. For example, you can do

.. ipython :: python

df.replace('.', nan)

to replace all occurrences of the string ``'.'`` with ``NaN``.

See the `full release notes
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
on GitHub for a complete list.
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6561,12 +6561,16 @@ def test_regex_replace_dict_nested(self):
dfmix = DataFrame(mix)
res = dfmix.replace({'b': {r'\s*\.\s*': nan}}, regex=True)
res2 = dfmix.copy()
res4 = dfmix.copy()
res2.replace({'b': {r'\s*\.\s*': nan}}, inplace=True, regex=True)
print res2
res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': nan}})
res4.replace(regex={'b': {r'\s*\.\s*': nan}}, inplace=True)
expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c':
mix['c']})
assert_frame_equal(res, expec)
assert_frame_equal(res2, expec)
assert_frame_equal(res3, expec)
assert_frame_equal(res4, expec)

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