From 04061452f7b4e5fcf87a7d996cd9a2965ac72782 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 7 Nov 2020 15:44:01 -0800 Subject: [PATCH] REF: make Series._replace_single a regular method --- pandas/core/generic.py | 37 +++++-------------------------------- pandas/core/series.py | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 36ce2c4776bd0..bea650c1b50fd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -137,36 +137,6 @@ ) -def _single_replace(self: "Series", to_replace, method, inplace, limit): - """ - Replaces values in a Series using the fill method specified when no - replacement value is given in the replace method - """ - if self.ndim != 1: - raise TypeError( - f"cannot replace {to_replace} with method {method} on a " - f"{type(self).__name__}" - ) - - orig_dtype = self.dtype - result = self if inplace else self.copy() - fill_f = missing.get_fill_func(method) - - mask = missing.mask_missing(result.values, to_replace) - values = fill_f(result.values, limit=limit, mask=mask) - - if values.dtype == orig_dtype and inplace: - return - - result = pd.Series(values, index=self.index, dtype=self.dtype).__finalize__(self) - - if inplace: - self._update_inplace(result) - return - - return result - - bool_t = bool # Need alias because NDFrame has def bool: @@ -6690,11 +6660,14 @@ def replace( if isinstance(to_replace, (tuple, list)): if isinstance(self, ABCDataFrame): + from pandas import Series + return self.apply( - _single_replace, args=(to_replace, method, inplace, limit) + Series._replace_single, + args=(to_replace, method, inplace, limit), ) self = cast("Series", self) - return _single_replace(self, to_replace, method, inplace, limit) + return self._replace_single(to_replace, method, inplace, limit) if not is_dict_like(to_replace): if not is_dict_like(regex): diff --git a/pandas/core/series.py b/pandas/core/series.py index e4a805a18bcdb..41adc1fc1ae93 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -67,7 +67,7 @@ remove_na_arraylike, ) -from pandas.core import algorithms, base, generic, nanops, ops +from pandas.core import algorithms, base, generic, missing, nanops, ops from pandas.core.accessor import CachedAccessor from pandas.core.aggregation import aggregate, transform from pandas.core.arrays import ExtensionArray @@ -4550,6 +4550,31 @@ def replace( method=method, ) + def _replace_single(self, to_replace, method, inplace, limit): + """ + Replaces values in a Series using the fill method specified when no + replacement value is given in the replace method + """ + + orig_dtype = self.dtype + result = self if inplace else self.copy() + fill_f = missing.get_fill_func(method) + + mask = missing.mask_missing(result.values, to_replace) + values = fill_f(result.values, limit=limit, mask=mask) + + if values.dtype == orig_dtype and inplace: + return + + result = self._constructor(values, index=self.index, dtype=self.dtype) + result = result.__finalize__(self) + + if inplace: + self._update_inplace(result) + return + + return result + @doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> "Series": return super().shift(