Skip to content

Commit dd7441b

Browse files
DOC: Fixes DataFrame.replace documentation (#55762)
* fix replace docs * fix replace docs * move description to examples * fix spacing * fix dicts * fix line size * fix whitespace * remove trailling whitespaces * fix df * fix df * Update pandas/core/shared_docs.py Co-authored-by: Matthew Roeschke <[email protected]> * Update pandas/core/shared_docs.py Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 2c1d4bb commit dd7441b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

pandas/core/shared_docs.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,7 @@
570570
.. deprecated:: 2.1.0
571571
regex : bool or same types as `to_replace`, default False
572572
Whether to interpret `to_replace` and/or `value` as regular
573-
expressions. If this is ``True`` then `to_replace` *must* be a
574-
string. Alternatively, this could be a regular expression or a
573+
expressions. Alternatively, this could be a regular expression or a
575574
list, dict, or array of regular expressions in which case
576575
`to_replace` must be ``None``.
577576
method : {{'pad', 'ffill', 'bfill'}}
@@ -790,6 +789,32 @@
790789
791790
.. versionchanged:: 1.4.0
792791
Previously the explicit ``None`` was silently ignored.
792+
793+
When ``regex=True``, ``value`` is not ``None`` and `to_replace` is a string,
794+
the replacement will be applied in all columns of the DataFrame.
795+
796+
>>> df = pd.DataFrame({{'A': [0, 1, 2, 3, 4],
797+
... 'B': ['a', 'b', 'c', 'd', 'e'],
798+
... 'C': ['f', 'g', 'h', 'i', 'j']}})
799+
800+
>>> df.replace(to_replace='^[a-g]', value = 'e', regex=True)
801+
A B C
802+
0 0 e e
803+
1 1 e e
804+
2 2 e h
805+
3 3 e i
806+
4 4 e j
807+
808+
If ``value`` is not ``None`` and `to_replace` is a dictionary, the dictionary
809+
keys will be the DataFrame columns that the replacement will be applied.
810+
811+
>>> df.replace(to_replace={{'B': '^[a-c]', 'C': '^[h-j]'}}, value = 'e', regex=True)
812+
A B C
813+
0 0 e f
814+
1 1 e g
815+
2 2 e e
816+
3 3 d e
817+
4 4 e e
793818
"""
794819

795820
_shared_docs[

0 commit comments

Comments
 (0)