Skip to content

Commit fcbdb7d

Browse files
authored
REF/TYP: consistent return type for Block.replace (#37010)
1 parent b50ffc8 commit fcbdb7d

File tree

1 file changed

+25
-69
lines changed

1 file changed

+25
-69
lines changed

pandas/core/internals/blocks.py

+25-69
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ def replace(
717717
inplace: bool = False,
718718
regex: bool = False,
719719
convert: bool = True,
720-
):
720+
) -> List["Block"]:
721721
"""
722722
replace the to_replace value with value, possible to create new
723723
blocks here this is just a call to putmask. regex is not used here.
@@ -808,9 +808,11 @@ def replace(
808808
)
809809
return blocks
810810

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

815817
def _replace_list(
816818
self,
@@ -852,16 +854,13 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
852854
to_replace=src,
853855
value=dest,
854856
inplace=inplace,
855-
convert=convert,
856857
regex=regex,
857858
)
858-
if m.any() or convert:
859-
if isinstance(result, list):
860-
new_rb.extend(result)
861-
else:
862-
new_rb.append(result)
863-
else:
864-
new_rb.append(blk)
859+
if convert and blk.is_object:
860+
result = extend_blocks(
861+
[b.convert(numeric=False, copy=True) for b in result]
862+
)
863+
new_rb.extend(result)
865864
rb = new_rb
866865
return rb
867866

@@ -1559,9 +1558,8 @@ def _replace_coerce(
15591558
value,
15601559
inplace: bool = True,
15611560
regex: bool = False,
1562-
convert: bool = False,
15631561
mask=None,
1564-
):
1562+
) -> List["Block"]:
15651563
"""
15661564
Replace value corresponding to the given boolean array with another
15671565
value.
@@ -1576,14 +1574,12 @@ def _replace_coerce(
15761574
Perform inplace modification.
15771575
regex : bool, default False
15781576
If true, perform regular expression substitution.
1579-
convert : bool, default True
1580-
If true, try to coerce any object types to better types.
15811577
mask : array-like of bool, optional
15821578
True indicate corresponding element is ignored.
15831579
15841580
Returns
15851581
-------
1586-
A new block if there is anything to replace or the original block.
1582+
List[Block]
15871583
"""
15881584
if mask.any():
15891585
if not regex:
@@ -1595,10 +1591,10 @@ def _replace_coerce(
15951591
value,
15961592
inplace=inplace,
15971593
regex=regex,
1598-
convert=convert,
1594+
convert=False,
15991595
mask=mask,
16001596
)
1601-
return self
1597+
return [self]
16021598

16031599

16041600
class ExtensionBlock(Block):
@@ -2479,14 +2475,16 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
24792475
def _can_hold_element(self, element: Any) -> bool:
24802476
return True
24812477

2482-
def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
2478+
def replace(
2479+
self, to_replace, value, inplace=False, regex=False, convert=True
2480+
) -> List["Block"]:
24832481
to_rep_is_list = is_list_like(to_replace)
24842482
value_is_list = is_list_like(value)
24852483
both_lists = to_rep_is_list and value_is_list
24862484
either_list = to_rep_is_list or value_is_list
24872485

2488-
result_blocks = []
2489-
blocks = [self]
2486+
result_blocks: List["Block"] = []
2487+
blocks: List["Block"] = [self]
24902488

24912489
if not either_list and is_re(to_replace):
24922490
return self._replace_single(
@@ -2503,7 +2501,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
25032501
result = b._replace_single(
25042502
to_rep, v, inplace=inplace, regex=regex, convert=convert
25052503
)
2506-
result_blocks = extend_blocks(result, result_blocks)
2504+
result_blocks.extend(result)
25072505
blocks = result_blocks
25082506
return result_blocks
25092507

@@ -2514,7 +2512,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
25142512
result = b._replace_single(
25152513
to_rep, value, inplace=inplace, regex=regex, convert=convert
25162514
)
2517-
result_blocks = extend_blocks(result, result_blocks)
2515+
result_blocks.extend(result)
25182516
blocks = result_blocks
25192517
return result_blocks
25202518

@@ -2524,7 +2522,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
25242522

25252523
def _replace_single(
25262524
self, to_replace, value, inplace=False, regex=False, convert=True, mask=None
2527-
):
2525+
) -> List["Block"]:
25282526
"""
25292527
Replace elements by the given value.
25302528
@@ -2545,7 +2543,7 @@ def _replace_single(
25452543
25462544
Returns
25472545
-------
2548-
a new block, the result after replacing
2546+
List[Block]
25492547
"""
25502548
inplace = validate_bool_kwarg(inplace, "inplace")
25512549

@@ -2619,48 +2617,6 @@ def re_replacer(s):
26192617
nbs = [block]
26202618
return nbs
26212619

2622-
def _replace_coerce(
2623-
self, to_replace, value, inplace=True, regex=False, convert=False, mask=None
2624-
):
2625-
"""
2626-
Replace value corresponding to the given boolean array with another
2627-
value.
2628-
2629-
Parameters
2630-
----------
2631-
to_replace : object or pattern
2632-
Scalar to replace or regular expression to match.
2633-
value : object
2634-
Replacement object.
2635-
inplace : bool, default False
2636-
Perform inplace modification.
2637-
regex : bool, default False
2638-
If true, perform regular expression substitution.
2639-
convert : bool, default True
2640-
If true, try to coerce any object types to better types.
2641-
mask : array-like of bool, optional
2642-
True indicate corresponding element is ignored.
2643-
2644-
Returns
2645-
-------
2646-
A new block if there is anything to replace or the original block.
2647-
"""
2648-
if mask.any():
2649-
nbs = super()._replace_coerce(
2650-
to_replace=to_replace,
2651-
value=value,
2652-
inplace=inplace,
2653-
regex=regex,
2654-
convert=convert,
2655-
mask=mask,
2656-
)
2657-
if convert:
2658-
nbs = extend_blocks([b.convert(numeric=False, copy=True) for b in nbs])
2659-
return nbs
2660-
if convert:
2661-
return self.convert(numeric=False, copy=True)
2662-
return [self]
2663-
26642620

26652621
class CategoricalBlock(ExtensionBlock):
26662622
__slots__ = ()
@@ -2672,12 +2628,12 @@ def replace(
26722628
inplace: bool = False,
26732629
regex: bool = False,
26742630
convert: bool = True,
2675-
):
2631+
) -> List["Block"]:
26762632
inplace = validate_bool_kwarg(inplace, "inplace")
26772633
result = self if inplace else self.copy()
26782634

26792635
result.values.replace(to_replace, value, inplace=True)
2680-
return result
2636+
return [result]
26812637

26822638

26832639
# -----------------------------------------------------------------

0 commit comments

Comments
 (0)