bigframes.pandas.Series.replace#

Series.replace(to_replace: Any, value: Any = None, *, regex: bool = False)[source]#

Replace values given in to_replace with value.

Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.

Examples:

>>> import bigframes.pandas as bpd
>>> s = bpd.Series([1, 2, 3, 4, 5])
>>> s
0    1
1    2
2    3
3    4
4    5
dtype: Int64
>>> s.replace(1, 5)
0    5
1    2
2    3
3    4
4    5
dtype: Int64

You can replace a list of values:

>>> s.replace([1, 3, 5], -1)
0    -1
1     2
2    -1
3     4
4    -1
dtype: Int64

You can use a replacement mapping:

>>> s.replace({1: 5, 3: 10})
0     5
1     2
2    10
3     4
4     5
dtype: Int64

With a string Series you can use a simple string replacement or a regex replacement:

>>> s = bpd.Series(["Hello", "Another Hello"])
>>> s.replace("Hello", "Hi")
0               Hi
1    Another Hello
dtype: string
>>> s.replace("Hello", "Hi", regex=True)
0            Hi
1    Another Hi
dtype: string
>>> s.replace("^Hello", "Hi", regex=True)
0               Hi
1    Another Hello
dtype: string
>>> s.replace("Hello$", "Hi", regex=True)
0            Hi
1    Another Hi
dtype: string
>>> s.replace("[Hh]e", "__", regex=True)
0            __llo
1    Anot__r __llo
dtype: string
Parameters:
  • to_replace (str, regex, list, int, float or None) –

    How to find the values that will be replaced.

    • numeric, str or regex:

      • numeric: numeric values equal to to_replace will be replaced with value

      • str: string exactly matching to_replace will be replaced with value

      • regex: regexs matching to_replace will be replaced with value

    • list of str, regex, or numeric:

      • First, if to_replace and value are both lists, they must be the same length.

      • Second, if regex=True then all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use.

      • str, regex and numeric rules apply as above.

  • value (scalar, default None) – Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed.

  • regex (bool, default False) – Whether to interpret to_replace and/or value as regular expressions. If this is True then to_replace must be a string.

Returns:

Object after replacement.

Return type:

bigframes.pandas.Series or bigframes.pandas.DataFrame

Raises:

TypeError

  • If to_replace is not a scalar, array-like, dict, or None * If to_replace is a dict and value is not a list, dict, ndarray, or Series * If to_replace is None and regex is not compilable into a regular expression or is a list, dict, ndarray, or Series. * When replacing multiple bool or datetime64 objects and the arguments to to_replace does not match the type of the value being replaced