Skip to content

REF: make Series._replace_single a regular method #37691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:


Expand Down Expand Up @@ -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):
Expand Down
27 changes: 26 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down