Skip to content

Commit 66e1daf

Browse files
committed
use _shared_docs
1 parent d72cd70 commit 66e1daf

File tree

3 files changed

+229
-399
lines changed

3 files changed

+229
-399
lines changed

pandas/core/frame.py

+1-212
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import warnings
2121
from textwrap import dedent
2222

23-
import pandas as pd
2423
import numpy as np
2524
import numpy.ma as ma
2625

@@ -43,7 +42,6 @@
4342
is_datetimetz,
4443
is_datetime64_any_dtype,
4544
is_datetime64tz_dtype,
46-
is_bool,
4745
is_bool_dtype,
4846
is_integer_dtype,
4947
is_float_dtype,
@@ -3082,218 +3080,9 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
30823080
inplace=inplace, limit=limit,
30833081
downcast=downcast, **kwargs)
30843082

3083+
@Appender(_shared_docs['replace'] % _shared_doc_kwargs)
30853084
def replace(self, to_replace=None, value=None, inplace=False, limit=None,
30863085
regex=False, method='pad', axis=None):
3087-
"""
3088-
Replace values given in 'to_replace' with 'value'.
3089-
3090-
Parameters
3091-
----------
3092-
to_replace : str, regex, list, dict, Series, numeric, or None
3093-
3094-
* numeric, str or regex:
3095-
3096-
- numeric: numeric values equal to ``to_replace`` will be
3097-
replaced with ``value``
3098-
- str: string exactly matching `to_replace` will be replaced
3099-
with ``value``
3100-
- regex: regexs matching ``to_replace`` will be replaced with
3101-
``value``
3102-
3103-
* list of str, regex, or numeric:
3104-
3105-
- First, if ``to_replace`` and ``value`` are both lists, they
3106-
**must** be the same length.
3107-
- Second, if ``regex=True`` then all of the strings in **both**
3108-
lists will be interpreted as regexs otherwise they will match
3109-
directly. This doesn't matter much for ``value`` since there
3110-
are only a few possible substitution regexes you can use.
3111-
- str and regex rules apply as above.
3112-
3113-
* dict:
3114-
3115-
- Dicts can be used to specify different replacement values
3116-
for different existing values. For example,
3117-
{'a': 'b', 'y': 'z'} replaces the value 'a' with 'b' and
3118-
'y' with 'z'. To use a dict in this way the ``value``
3119-
parameter should be ``None``.
3120-
- Alternatively, a dict can specify that different values
3121-
should be replaced in different columns. For example,
3122-
{'a': 1, 'b': 'z'} looks for the value 1 in column 'a' and
3123-
the value 'z' in column 'b' and replaces these values with
3124-
whatever is specified in ``value``. The ``value`` parameter
3125-
should not be ``None`` in this case. You can treat this as a
3126-
special case of passing two lists except that you are
3127-
specifying the column to search in.
3128-
- Nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as
3129-
follows: look in column 'a' for the value 'b' and replace it
3130-
with NaN. The ``value`` parameter should be ``None`` to use
3131-
a nested dict in this way. You can nest regular expressions
3132-
as well. Note that column names (the top-level dictionary
3133-
keys in a nested dictionary) **cannot** be regular
3134-
expressions.
3135-
3136-
* None:
3137-
3138-
- This means that the ``regex`` argument must be a string,
3139-
compiled regular expression, or list, dict, ndarray or Series
3140-
of such elements. If ``value`` is also ``None`` then this
3141-
**must** be a nested dictionary or ``Series``.
3142-
3143-
See the examples section for examples of each of these.
3144-
value : scalar, dict, list, str, regex, default None
3145-
Value to replace any values matching ``to_replace`` with.
3146-
Alternatively, a dict of values specifying which value to use for
3147-
each column (columns not in the dict will not be filled). Regular
3148-
expressions, strings and lists or dicts of such objects are also
3149-
allowed.
3150-
inplace : boolean, default False
3151-
If True, in place. Note: this will modify any
3152-
other views on this object (e.g. a column from a DataFrame).
3153-
Returns the caller if this is True.
3154-
limit : int, default None
3155-
Maximum size gap to forward or backward fill
3156-
regex : bool or same types as `to_replace`, default False
3157-
Whether to interpret ``to_replace`` and/or ``value`` as regular
3158-
expressions. If this is ``True`` then ``to_replace`` *must* be a
3159-
string. Alternatively, this could be a regular expression or a
3160-
list, dict, or array of regular expressions in which case
3161-
``to_replace`` must be ``None``.
3162-
method : string, optional, {'pad', 'ffill', 'bfill'}
3163-
The method to use when for replacement, when ``to_replace`` is a
3164-
``list``.
3165-
3166-
See Also
3167-
--------
3168-
DataFrame.fillna : Fill NA/NaN values
3169-
DataFrame.where : Replace values based on boolean condition
3170-
3171-
Returns
3172-
-------
3173-
filled : DataFrame
3174-
3175-
Raises
3176-
------
3177-
AssertionError
3178-
* If ``regex`` is not a ``bool`` and ``to_replace`` is not
3179-
``None``.
3180-
TypeError
3181-
* If ``to_replace`` is a ``dict`` and `value` is not a ``list``,
3182-
``dict``, ``ndarray``, or ``Series``
3183-
* If ``to_replace`` is ``None`` and ``regex`` is not compilable
3184-
into a regular expression or is a list, dict, ndarray, or
3185-
Series.
3186-
* When replacing multiple ``bool`` or ``datetime64`` objects and
3187-
the arguments to `to_replace` does not match the type of the
3188-
value being replaced
3189-
ValueError
3190-
* If a ``list`` or an ``ndarray`` is passed to `to_replace` and
3191-
`value` but they are not the same length.
3192-
3193-
Notes
3194-
-----
3195-
* Regex substitution is performed under the hood with ``re.sub``. The
3196-
rules for substitution for ``re.sub`` are the same.
3197-
* Regular expressions will only substitute on strings, meaning you
3198-
cannot provide, for example, a regular expression matching floating
3199-
point numbers and expect the columns in your frame that have a
3200-
numeric dtype to be matched. However, if those floating point
3201-
numbers *are* strings, then you can do this.
3202-
* This method has *a lot* of options. You are encouraged to experiment
3203-
and play with this method to gain intuition about how it works.
3204-
3205-
Examples
3206-
--------
3207-
3208-
>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
3209-
... 'B': [5, 6, 7, 8, 9],
3210-
... 'C': ['a', 'b', 'c', 'd', 'e']})
3211-
>>> df.replace(0, 5)
3212-
A B C
3213-
0 5 5 a
3214-
1 1 6 b
3215-
2 2 7 c
3216-
3 3 8 d
3217-
4 4 9 e
3218-
3219-
>>> df.replace([0, 1, 2, 3], 4)
3220-
A B C
3221-
0 4 5 a
3222-
1 4 6 b
3223-
2 4 7 c
3224-
3 4 8 d
3225-
4 4 9 e
3226-
>>> df.replace([0, 1, 2, 3], [4, 3, 2, 1])
3227-
A B C
3228-
0 4 5 a
3229-
1 3 6 b
3230-
2 2 7 c
3231-
3 1 8 d
3232-
4 4 9 e
3233-
3234-
>>> df.replace({0: 10, 1: 100})
3235-
A B C
3236-
0 10 5 a
3237-
1 100 6 b
3238-
2 2 7 c
3239-
3 3 8 d
3240-
4 4 9 e
3241-
>>> df.replace({'A': 0, 'B': 5}, 100)
3242-
A B C
3243-
0 100 100 a
3244-
1 1 6 b
3245-
2 2 7 c
3246-
3 3 8 d
3247-
4 4 9 e
3248-
>>> df.replace({'A': {0: 100, 4: 400}})
3249-
A B C
3250-
0 100 5 a
3251-
1 1 6 b
3252-
2 2 7 c
3253-
3 3 8 d
3254-
4 400 9 e
3255-
3256-
>>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],
3257-
... 'B': ['abc', 'bar', 'xyz']})
3258-
>>> df.replace(to_replace=r'^ba.$', value='new', regex=True)
3259-
A B
3260-
0 new abc
3261-
1 foo new
3262-
2 bait xyz
3263-
>>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)
3264-
A B
3265-
0 new abc
3266-
1 foo bar
3267-
2 bait xyz
3268-
>>> df.replace(regex=r'^ba.$', value='new')
3269-
A B
3270-
0 new abc
3271-
1 foo new
3272-
2 bait xyz
3273-
>>> df.replace(regex={r'^ba.$':'new', 'foo':'xyz'})
3274-
A B
3275-
0 new abc
3276-
1 xyz new
3277-
2 bait xyz
3278-
>>> df.replace(regex=[r'^ba.$', 'foo'], value='new')
3279-
A B
3280-
0 new abc
3281-
1 new new
3282-
2 bait xyz
3283-
3284-
Note that when replacing multiple ``bool`` or ``datetime64`` objects,
3285-
the data types in the ``to_replace`` parameter must match the data
3286-
type of the value being replaced:
3287-
3288-
>>> df = pd.DataFrame({'A': [True, False, True],
3289-
... 'B': [False, True, False]})
3290-
>>> df.replace({'a string': 'new value', True: False}) # raises
3291-
TypeError: Cannot compare types 'ndarray(dtype=bool)' and 'str'
3292-
3293-
This raises a ``TypeError`` because one of the ``dict`` keys is not of
3294-
the correct type for replacement.
3295-
3296-
"""
32973086
return super(DataFrame, self).replace(to_replace=to_replace,
32983087
value=value, inplace=inplace,
32993088
limit=limit, regex=regex,

0 commit comments

Comments
 (0)