Skip to content

REF/TYP: consistent return type for Block.replace #37010

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
Oct 10, 2020
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
94 changes: 25 additions & 69 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def replace(
inplace: bool = False,
regex: bool = False,
convert: bool = True,
):
) -> List["Block"]:
"""
replace the to_replace value with value, possible to create new
blocks here this is just a call to putmask. regex is not used here.
Expand Down Expand Up @@ -796,9 +796,11 @@ def replace(
)
return blocks

def _replace_single(self, *args, **kwargs):
def _replace_single(
self, to_replace, value, inplace=False, regex=False, convert=True, mask=None
) -> List["Block"]:
""" no-op on a non-ObjectBlock """
return self if kwargs["inplace"] else self.copy()
return [self] if inplace else [self.copy()]

def _replace_list(
self,
Expand Down Expand Up @@ -840,16 +842,13 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
to_replace=src,
value=dest,
inplace=inplace,
convert=convert,
regex=regex,
)
if m.any() or convert:
if isinstance(result, list):
new_rb.extend(result)
else:
new_rb.append(result)
else:
new_rb.append(blk)
if convert and blk.is_object:
result = extend_blocks(
[b.convert(numeric=False, copy=True) for b in result]
)
new_rb.extend(result)
rb = new_rb
return rb

Expand Down Expand Up @@ -1547,9 +1546,8 @@ def _replace_coerce(
value,
inplace: bool = True,
regex: bool = False,
convert: bool = False,
mask=None,
):
) -> List["Block"]:
"""
Replace value corresponding to the given boolean array with another
value.
Expand All @@ -1564,14 +1562,12 @@ def _replace_coerce(
Perform inplace modification.
regex : bool, default False
If true, perform regular expression substitution.
convert : bool, default True
If true, try to coerce any object types to better types.
mask : array-like of bool, optional
True indicate corresponding element is ignored.

Returns
-------
A new block if there is anything to replace or the original block.
List[Block]
"""
if mask.any():
if not regex:
Expand All @@ -1583,10 +1579,10 @@ def _replace_coerce(
value,
inplace=inplace,
regex=regex,
convert=convert,
convert=False,
mask=mask,
)
return self
return [self]


class ExtensionBlock(Block):
Expand Down Expand Up @@ -2488,14 +2484,16 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
def _can_hold_element(self, element: Any) -> bool:
return True

def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
def replace(
self, to_replace, value, inplace=False, regex=False, convert=True
) -> List["Block"]:
to_rep_is_list = is_list_like(to_replace)
value_is_list = is_list_like(value)
both_lists = to_rep_is_list and value_is_list
either_list = to_rep_is_list or value_is_list

result_blocks = []
blocks = [self]
result_blocks: List["Block"] = []
blocks: List["Block"] = [self]

if not either_list and is_re(to_replace):
return self._replace_single(
Expand All @@ -2512,7 +2510,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
result = b._replace_single(
to_rep, v, inplace=inplace, regex=regex, convert=convert
)
result_blocks = extend_blocks(result, result_blocks)
result_blocks.extend(result)
blocks = result_blocks
return result_blocks

Expand All @@ -2523,7 +2521,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
result = b._replace_single(
to_rep, value, inplace=inplace, regex=regex, convert=convert
)
result_blocks = extend_blocks(result, result_blocks)
result_blocks.extend(result)
blocks = result_blocks
return result_blocks

Expand All @@ -2533,7 +2531,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):

def _replace_single(
self, to_replace, value, inplace=False, regex=False, convert=True, mask=None
):
) -> List["Block"]:
"""
Replace elements by the given value.

Expand All @@ -2554,7 +2552,7 @@ def _replace_single(

Returns
-------
a new block, the result after replacing
List[Block]
"""
inplace = validate_bool_kwarg(inplace, "inplace")

Expand Down Expand Up @@ -2628,48 +2626,6 @@ def re_replacer(s):
nbs = [block]
return nbs

def _replace_coerce(
self, to_replace, value, inplace=True, regex=False, convert=False, mask=None
):
"""
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.
inplace : bool, default False
Perform inplace modification.
regex : bool, default False
If true, perform regular expression substitution.
convert : bool, default True
If true, try to coerce any object types to better types.
mask : array-like of bool, optional
True indicate corresponding element is ignored.

Returns
-------
A new block if there is anything to replace or the original block.
"""
if mask.any():
nbs = super()._replace_coerce(
to_replace=to_replace,
value=value,
inplace=inplace,
regex=regex,
convert=convert,
mask=mask,
)
if convert:
nbs = extend_blocks([b.convert(numeric=False, copy=True) for b in nbs])
return nbs
if convert:
return self.convert(numeric=False, copy=True)
return [self]


class CategoricalBlock(ExtensionBlock):
__slots__ = ()
Expand All @@ -2681,12 +2637,12 @@ def replace(
inplace: bool = False,
regex: bool = False,
convert: bool = True,
):
) -> List["Block"]:
inplace = validate_bool_kwarg(inplace, "inplace")
result = self if inplace else self.copy()

result.values.replace(to_replace, value, inplace=True)
return result
return [result]


# -----------------------------------------------------------------
Expand Down