Skip to content

Commit 082f1a6

Browse files
committed
DOC: Improve replace docstring (pandas-dev#17673)
1 parent e57b142 commit 082f1a6

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

pandas/core/generic.py

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4362,8 +4362,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
43624362
----------
43634363
to_replace : str, regex, list, dict, Series, numeric, or None
43644364
4365-
* str or regex:
4366-
4365+
* numeric, str or regex:
4366+
- numeric: numeric values equal to `to_replace` will be
4367+
replaced with `value`
43674368
- str: string exactly matching `to_replace` will be replaced
43684369
with `value`
43694370
- regex: regexs matching `to_replace` will be replaced with
@@ -4381,14 +4382,16 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
43814382
43824383
* dict:
43834384
4385+
- Keys map to column names and values map to substitution
4386+
values. For example, {'a': 1, 'b': 'z'} looks for the value 1
4387+
in column 'a' and the value 'z' in column 'b'. You can treat
4388+
this as a special case of passing two lists except that you
4389+
are specifying the column to search in.
43844390
- Nested dictionaries, e.g., {'a': {'b': nan}}, are read as
43854391
follows: look in column 'a' for the value 'b' and replace it
4386-
with nan. You can nest regular expressions as well. Note that
4392+
with NaN. You can nest regular expressions as well. Note that
43874393
column names (the top-level dictionary keys in a nested
43884394
dictionary) **cannot** be regular expressions.
4389-
- Keys map to column names and values map to substitution
4390-
values. You can treat this as a special case of passing two
4391-
lists except that you are specifying the column to search in.
43924395
43934396
* None:
43944397
@@ -4421,13 +4424,12 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
44214424
44224425
See Also
44234426
--------
4424-
NDFrame.reindex
4425-
NDFrame.asfreq
4426-
NDFrame.fillna
4427+
:func:`DataFrame.fillna` : Fill NA/NaN values
4428+
:func:`DataFrame.where` : Replace values based on boolean condition
44274429
44284430
Returns
44294431
-------
4430-
filled : NDFrame
4432+
filled : DataFrame
44314433
44324434
Raises
44334435
------
@@ -4438,6 +4440,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
44384440
``dict``, ``ndarray``, or ``Series``
44394441
* If `to_replace` is ``None`` and `regex` is not compilable into a
44404442
regular expression or is a list, dict, ndarray, or Series.
4443+
* When replacing multiple `bool` or `datetime64` objects and the
4444+
the arguments to `to_replace` does not match the type of the
4445+
value being replaced
44414446
ValueError
44424447
* If `to_replace` and `value` are ``list`` s or ``ndarray`` s, but
44434448
they are not the same length.
@@ -4453,6 +4458,63 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
44534458
*are* strings, then you can do this.
44544459
* This method has *a lot* of options. You are encouraged to experiment
44554460
and play with this method to gain intuition about how it works.
4461+
4462+
Examples
4463+
--------
4464+
Simple Cases
4465+
4466+
Scalar Value
4467+
4468+
>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
4469+
... 'B': [5, 6, 7, 8, 9],
4470+
... 'C': ['a', 'b', 'c', 'd', 'e']})
4471+
>>> df.replace(0, 5)
4472+
A B C
4473+
0 5 5 a
4474+
1 1 6 b
4475+
2 2 7 c
4476+
3 3 8 d
4477+
4 4 9 e
4478+
4479+
Lists
4480+
4481+
>>> df.replace([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])
4482+
A B C
4483+
0 4 5 a
4484+
1 3 6 b
4485+
2 2 7 c
4486+
3 1 8 d
4487+
4 0 9 e
4488+
4489+
Simple Dict
4490+
4491+
>>> df.replace({0: 10, 1: 100})
4492+
A B C
4493+
0 10 5 a
4494+
1 100 6 b
4495+
2 2 7 c
4496+
3 3 8 d
4497+
4 4 9 e
4498+
4499+
Dict for values by column
4500+
4501+
>>> df.replace({'A': 0, 'B': 5}, 100)
4502+
A B C
4503+
0 100 100 a
4504+
1 1 6 b
4505+
2 2 7 c
4506+
3 3 8 d
4507+
4 4 9 e
4508+
4509+
regex
4510+
4511+
>>> df = {'a': list(range(4)), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']}
4512+
>>> df.replace(r'\s*\.\s*', np.nan, regex=True)
4513+
a b c
4514+
0 0 a a
4515+
1 1 b b
4516+
2 2 NaN NaN
4517+
3 3 NaN d
44564518
44574519
"""
44584520
inplace = validate_bool_kwarg(inplace, 'inplace')

0 commit comments

Comments
 (0)