Skip to content

Commit b3c2479

Browse files
committed
Documentation additions/fixes.
1 parent c57f559 commit b3c2479

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

doc/source/text.rst

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ i.e., from the end of the string to the beginning of the string:
118118
119119
s2.str.rsplit('_', expand=True, n=1)
120120
121-
Methods like ``replace`` and ``findall`` take `regular expressions
122-
<https://docs.python.org/3/library/re.html>`__, too:
121+
``replace`` by default replaces `regular expressions
122+
<https://docs.python.org/3/library/re.html>`__:
123123

124124
.. ipython:: python
125125
@@ -146,12 +146,25 @@ following code will cause trouble because of the regular expression meaning of
146146
# We need to escape the special character (for >1 len patterns)
147147
dollars.str.replace(r'-\$', '-')
148148
149+
.. versionadded:: 0.23.0
150+
151+
If you do want literal replacement of a string (equivalent to
152+
:meth:`str.replace`), you can set the optional ``regex`` parameter to
153+
``False``, rather than escaping each character. In this case both ``pat``
154+
and ``repl`` must be strings:
155+
156+
.. ipython:: python
157+
158+
# These lines are equivalent
159+
dollars.str.replace(r'-\$', '-')
160+
dollars.str.replace('-$', '-', regex=False)
161+
162+
.. versionadded:: 0.20.0
163+
149164
The ``replace`` method can also take a callable as replacement. It is called
150165
on every ``pat`` using :func:`re.sub`. The callable should expect one
151166
positional argument (a regex object) and return a string.
152167

153-
.. versionadded:: 0.20.0
154-
155168
.. ipython:: python
156169
157170
# Reverse every lowercase alphabetic word
@@ -164,12 +177,12 @@ positional argument (a regex object) and return a string.
164177
repl = lambda m: m.group('two').swapcase()
165178
pd.Series(['Foo Bar Baz', np.nan]).str.replace(pat, repl)
166179
180+
.. versionadded:: 0.20.0
181+
167182
The ``replace`` method also accepts a compiled regular expression object
168183
from :func:`re.compile` as a pattern. All flags should be included in the
169184
compiled regular expression object.
170185

171-
.. versionadded:: 0.20.0
172-
173186
.. ipython:: python
174187
175188
import re
@@ -186,6 +199,7 @@ regular expression object will raise a ``ValueError``.
186199
---------------------------------------------------------------------------
187200
ValueError: case and flags cannot be set when pat is a compiled regex
188201

202+
189203
Indexing with ``.str``
190204
----------------------
191205

@@ -432,7 +446,7 @@ Method Summary
432446
:meth:`~Series.str.join`;Join strings in each element of the Series with passed separator
433447
:meth:`~Series.str.get_dummies`;Split strings on the delimiter returning DataFrame of dummy variables
434448
:meth:`~Series.str.contains`;Return boolean array if each string contains pattern/regex
435-
:meth:`~Series.str.replace`;Replace occurrences of pattern/regex with some other string or the return value of a callable given the occurrence
449+
:meth:`~Series.str.replace`;Replace occurrences of pattern/regex/string with some other string or the return value of a callable given the occurrence
436450
:meth:`~Series.str.repeat`;Duplicate values (``s.str.repeat(3)`` equivalent to ``x * 3``)
437451
:meth:`~Series.str.pad`;"Add whitespace to left, right, or both sides of strings"
438452
:meth:`~Series.str.center`;Equivalent to ``str.center``

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ Other API Changes
620620
- Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`)
621621
- :class:`DateOffset` objects render more simply, e.g. ``<DateOffset: days=1>`` instead of ``<DateOffset: kwds={'days': 1}>`` (:issue:`19403`)
622622
- ``Categorical.fillna`` now validates its ``value`` and ``method`` keyword arguments. It now raises when both or none are specified, matching the behavior of :meth:`Series.fillna` (:issue:`19682`)
623+
- :func:`Series.str.replace` now takes an optional `regex` keyword which, when set to ``False``, uses literal string replacement rather than regex replacement (:issue:`16808`)
623624

624625
.. _whatsnew_0230.deprecations:
625626

pandas/core/strings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=True):
368368
369369
When `pat` is a string and `regex` is True, the given `pat` is compiled
370370
as a regex. When `repl` is a string, it replaces matching regex patterns
371-
literally as with :meth:`re.sub`:
371+
as with :meth:`re.sub`:
372+
372373
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
373374
0 bao
374375
1 baz

0 commit comments

Comments
 (0)