Skip to content

Commit ae54325

Browse files
authored
REF: make Series._replace_single a regular method (#37691)
1 parent ea706b3 commit ae54325

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

pandas/core/generic.py

+5-32
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,6 @@
137137
)
138138

139139

140-
def _single_replace(self: "Series", to_replace, method, inplace, limit):
141-
"""
142-
Replaces values in a Series using the fill method specified when no
143-
replacement value is given in the replace method
144-
"""
145-
if self.ndim != 1:
146-
raise TypeError(
147-
f"cannot replace {to_replace} with method {method} on a "
148-
f"{type(self).__name__}"
149-
)
150-
151-
orig_dtype = self.dtype
152-
result = self if inplace else self.copy()
153-
fill_f = missing.get_fill_func(method)
154-
155-
mask = missing.mask_missing(result.values, to_replace)
156-
values = fill_f(result.values, limit=limit, mask=mask)
157-
158-
if values.dtype == orig_dtype and inplace:
159-
return
160-
161-
result = pd.Series(values, index=self.index, dtype=self.dtype).__finalize__(self)
162-
163-
if inplace:
164-
self._update_inplace(result)
165-
return
166-
167-
return result
168-
169-
170140
bool_t = bool # Need alias because NDFrame has def bool:
171141

172142

@@ -6690,11 +6660,14 @@ def replace(
66906660

66916661
if isinstance(to_replace, (tuple, list)):
66926662
if isinstance(self, ABCDataFrame):
6663+
from pandas import Series
6664+
66936665
return self.apply(
6694-
_single_replace, args=(to_replace, method, inplace, limit)
6666+
Series._replace_single,
6667+
args=(to_replace, method, inplace, limit),
66956668
)
66966669
self = cast("Series", self)
6697-
return _single_replace(self, to_replace, method, inplace, limit)
6670+
return self._replace_single(to_replace, method, inplace, limit)
66986671

66996672
if not is_dict_like(to_replace):
67006673
if not is_dict_like(regex):

pandas/core/series.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
remove_na_arraylike,
6868
)
6969

70-
from pandas.core import algorithms, base, generic, nanops, ops
70+
from pandas.core import algorithms, base, generic, missing, nanops, ops
7171
from pandas.core.accessor import CachedAccessor
7272
from pandas.core.aggregation import aggregate, transform
7373
from pandas.core.arrays import ExtensionArray
@@ -4558,6 +4558,31 @@ def replace(
45584558
method=method,
45594559
)
45604560

4561+
def _replace_single(self, to_replace, method, inplace, limit):
4562+
"""
4563+
Replaces values in a Series using the fill method specified when no
4564+
replacement value is given in the replace method
4565+
"""
4566+
4567+
orig_dtype = self.dtype
4568+
result = self if inplace else self.copy()
4569+
fill_f = missing.get_fill_func(method)
4570+
4571+
mask = missing.mask_missing(result.values, to_replace)
4572+
values = fill_f(result.values, limit=limit, mask=mask)
4573+
4574+
if values.dtype == orig_dtype and inplace:
4575+
return
4576+
4577+
result = self._constructor(values, index=self.index, dtype=self.dtype)
4578+
result = result.__finalize__(self)
4579+
4580+
if inplace:
4581+
self._update_inplace(result)
4582+
return
4583+
4584+
return result
4585+
45614586
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"])
45624587
def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> "Series":
45634588
return super().shift(

0 commit comments

Comments
 (0)