Skip to content

Commit b1a5591

Browse files
committed
ENH: examples for release notes and fix up nested dict example and test
fix up rough edges of docs and add a more thorough test for nested dicts fix unintended example
1 parent bae62ea commit b1a5591

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
@@ -59,6 +59,24 @@ Enhancements
5959
``Series`` with object dtype. See the examples section in the regular docs
6060
:ref:`Replacing via String Expression <missing_data.replace_expression>`
6161

62+
For example you can do
63+
64+
.. ipython :: python
65+
66+
df = DataFrame({'a': list('ab..'), 'b': [1, 2, 3, 4]})
67+
df.replace(regex=r'\s*\.\s*', value=nan)
68+
69+
to replace all occurrences of the string ``'.'`` with zero or more
70+
instances of surrounding whitespace with ``NaN``.
71+
72+
Regular string replacement still works as expected. For example, you can do
73+
74+
.. ipython :: python
75+
76+
df.replace('.', nan)
77+
78+
to replace all occurrences of the string ``'.'`` with ``NaN``.
79+
6280
See the `full release notes
6381
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
6482
on GitHub for a complete list.

pandas/tests/test_frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -6561,12 +6561,16 @@ def test_regex_replace_dict_nested(self):
65616561
dfmix = DataFrame(mix)
65626562
res = dfmix.replace({'b': {r'\s*\.\s*': nan}}, regex=True)
65636563
res2 = dfmix.copy()
6564+
res4 = dfmix.copy()
65646565
res2.replace({'b': {r'\s*\.\s*': nan}}, inplace=True, regex=True)
6565-
print res2
6566+
res3 = dfmix.replace(regex={'b': {r'\s*\.\s*': nan}})
6567+
res4.replace(regex={'b': {r'\s*\.\s*': nan}}, inplace=True)
65666568
expec = DataFrame({'a': mix['a'], 'b': ['a', 'b', nan, nan], 'c':
65676569
mix['c']})
65686570
assert_frame_equal(res, expec)
65696571
assert_frame_equal(res2, expec)
6572+
assert_frame_equal(res3, expec)
6573+
assert_frame_equal(res4, expec)
65706574

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

0 commit comments

Comments
 (0)