Skip to content

REF: put Block replace methods together #39810

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
Feb 15, 2021
Merged
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
103 changes: 54 additions & 49 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ def copy(self, deep: bool = True):
values = values.copy()
return self.make_block_same_class(values, ndim=self.ndim)

# ---------------------------------------------------------------------
# Replace

def replace(
self,
to_replace,
Expand Down Expand Up @@ -872,6 +875,57 @@ def _replace_list(
rb = new_rb
return rb

def _replace_coerce(
self,
to_replace,
value,
mask: np.ndarray,
inplace: bool = True,
regex: bool = False,
) -> List[Block]:
"""
Replace value corresponding to the given boolean array with another
value.

Parameters
----------
to_replace : object or pattern
Scalar to replace or regular expression to match.
value : object
Replacement object.
mask : np.ndarray[bool]
True indicate corresponding element is ignored.
inplace : bool, default True
Perform inplace modification.
regex : bool, default False
If true, perform regular expression substitution.

Returns
-------
List[Block]
"""
if mask.any():
if not regex:
nb = self.coerce_to_target_dtype(value)
if nb is self and not inplace:
nb = nb.copy()
putmask_inplace(nb.values, mask, value)
return [nb]
else:
regex = should_use_regex(regex, to_replace)
if regex:
return self._replace_regex(
to_replace,
value,
inplace=inplace,
convert=False,
mask=mask,
)
return self.replace(to_replace, value, inplace=inplace, regex=False)
return [self]

# ---------------------------------------------------------------------

def setitem(self, indexer, value):
"""
Attempt self.values[indexer] = value, possibly creating a new array.
Expand Down Expand Up @@ -1402,55 +1456,6 @@ def quantile(

return make_block(result, placement=self.mgr_locs, ndim=2)

def _replace_coerce(
self,
to_replace,
value,
mask: np.ndarray,
inplace: bool = True,
regex: bool = False,
) -> List[Block]:
"""
Replace value corresponding to the given boolean array with another
value.

Parameters
----------
to_replace : object or pattern
Scalar to replace or regular expression to match.
value : object
Replacement object.
mask : np.ndarray[bool]
True indicate corresponding element is ignored.
inplace : bool, default True
Perform inplace modification.
regex : bool, default False
If true, perform regular expression substitution.

Returns
-------
List[Block]
"""
if mask.any():
if not regex:
nb = self.coerce_to_target_dtype(value)
if nb is self and not inplace:
nb = nb.copy()
putmask_inplace(nb.values, mask, value)
return [nb]
else:
regex = should_use_regex(regex, to_replace)
if regex:
return self._replace_regex(
to_replace,
value,
inplace=inplace,
convert=False,
mask=mask,
)
return self.replace(to_replace, value, inplace=inplace, regex=False)
return [self]


class ExtensionBlock(Block):
"""
Expand Down