Skip to content

Commit c468d2f

Browse files
committed
Merge pull request #3639 from cpcloud/replace-with-regex-2285
single example for release notes
2 parents 7bc7c82 + b1a5591 commit c468d2f

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

doc/source/missing_data.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ or you can pass the nested dictionary like so
406406

407407
.. ipython:: python
408408
409-
df.replace(regex={'b': {'b': r'\s*\.\s*'}})
409+
df.replace(regex={'b': {r'\s*\.\s*': nan}})
410410
411411
You can also use the group of a regular expression match when replacing (dict
412412
of regex -> dict of regex), this works for lists as well
@@ -420,7 +420,7 @@ will be replaced with a scalar (list of regex -> regex)
420420

421421
.. ipython:: python
422422
423-
df.replace([r'\s*\.\*', r'a|b'], nan, regex=True)
423+
df.replace([r'\s*\.\s*', r'a|b'], nan, regex=True)
424424
425425
All of the regular expression examples can also be passed with the
426426
``to_replace`` argument as the ``regex`` argument. In this case the ``value``
@@ -429,7 +429,7 @@ dictionary. The previous example, in this case, would then be
429429

430430
.. ipython:: python
431431
432-
df.replace(regex=[r'\s*\.\*', r'a|b'], value=nan)
432+
df.replace(regex=[r'\s*\.\s*', r'a|b'], value=nan)
433433
434434
This can be convenient if you do not want to pass ``regex=True`` every time you
435435
want to use a regular expression.

doc/source/v0.11.1.txt

+18
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ Bug Fixes
155155
- Duplicate indexes with and empty DataFrame.from_records will return a correct frame (GH3562_)
156156
- Concat to produce a non-unique columns when duplicates are across dtypes is fixed (GH3602_)
157157

158+
For example you can do
159+
160+
.. ipython :: python
161+
162+
df = DataFrame({'a': list('ab..'), 'b': [1, 2, 3, 4]})
163+
df.replace(regex=r'\s*\.\s*', value=nan)
164+
165+
to replace all occurrences of the string ``'.'`` with zero or more
166+
instances of surrounding whitespace with ``NaN``.
167+
168+
Regular string replacement still works as expected. For example, you can do
169+
170+
.. ipython :: python
171+
172+
df.replace('.', nan)
173+
174+
to replace all occurrences of the string ``'.'`` with ``NaN``.
175+
158176
See the `full release notes
159177
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
160178
on GitHub for a complete list.

pandas/tests/test_frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -6653,12 +6653,16 @@ def test_regex_replace_dict_nested(self):
66536653
dfmix = DataFrame(mix)
66546654
res = dfmix.replace({'b': {r'\s*\.\s*': nan}}, regex=True)
66556655
res2 = dfmix.copy()
6656+
res4 = dfmix.copy()
66566657
res2.replace({'b': {r'\s*\.\s*': nan}}, inplace=True, regex=True)
6657-
print res2
6658+
res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': nan}})
6659+
res4.replace(regex={'b': {r'\s*\.\s*': nan}}, inplace=True)
66586660
expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c':
66596661
mix['c']})
66606662
assert_frame_equal(res, expec)
66616663
assert_frame_equal(res2, expec)
6664+
assert_frame_equal(res3, expec)
6665+
assert_frame_equal(res4, expec)
66626666

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

0 commit comments

Comments
 (0)