Skip to content

Commit 188258b

Browse files
authored
CLN: _replace_single (#37683)
* REF: simplify _replace_single by noting regex kwarg is bool * Annotate * CLN: remove never-False convert kwarg
1 parent f113e46 commit 188258b

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed

pandas/core/internals/blocks.py

+34-48
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ def replace(
726726
value,
727727
inplace: bool = False,
728728
regex: bool = False,
729-
convert: bool = True,
730729
) -> List["Block"]:
731730
"""
732731
replace the to_replace value with value, possible to create new
@@ -755,9 +754,7 @@ def replace(
755754
if len(to_replace) == 1:
756755
# _can_hold_element checks have reduced this back to the
757756
# scalar case and we can avoid a costly object cast
758-
return self.replace(
759-
to_replace[0], value, inplace=inplace, regex=regex, convert=convert
760-
)
757+
return self.replace(to_replace[0], value, inplace=inplace, regex=regex)
761758

762759
# GH 22083, TypeError or ValueError occurred within error handling
763760
# causes infinite loop. Cast and retry only if not objectblock.
@@ -771,7 +768,6 @@ def replace(
771768
value=value,
772769
inplace=inplace,
773770
regex=regex,
774-
convert=convert,
775771
)
776772

777773
values = self.values
@@ -810,16 +806,21 @@ def replace(
810806
value=value,
811807
inplace=inplace,
812808
regex=regex,
813-
convert=convert,
814-
)
815-
if convert:
816-
blocks = extend_blocks(
817-
[b.convert(numeric=False, copy=not inplace) for b in blocks]
818809
)
810+
811+
blocks = extend_blocks(
812+
[b.convert(numeric=False, copy=not inplace) for b in blocks]
813+
)
819814
return blocks
820815

821816
def _replace_single(
822-
self, to_replace, value, inplace=False, regex=False, convert=True, mask=None
817+
self,
818+
to_replace,
819+
value,
820+
inplace: bool = False,
821+
regex: bool = False,
822+
convert: bool = True,
823+
mask=None,
823824
) -> List["Block"]:
824825
""" no-op on a non-ObjectBlock """
825826
return [self] if inplace else [self.copy()]
@@ -860,9 +861,9 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
860861
m = masks[i]
861862
convert = i == src_len # only convert once at the end
862863
result = blk._replace_coerce(
863-
mask=m,
864864
to_replace=src,
865865
value=dest,
866+
mask=m,
866867
inplace=inplace,
867868
regex=regex,
868869
)
@@ -1567,9 +1568,9 @@ def _replace_coerce(
15671568
self,
15681569
to_replace,
15691570
value,
1571+
mask: np.ndarray,
15701572
inplace: bool = True,
15711573
regex: bool = False,
1572-
mask=None,
15731574
) -> List["Block"]:
15741575
"""
15751576
Replace value corresponding to the given boolean array with another
@@ -1581,12 +1582,12 @@ def _replace_coerce(
15811582
Scalar to replace or regular expression to match.
15821583
value : object
15831584
Replacement object.
1585+
mask : np.ndarray[bool]
1586+
True indicate corresponding element is ignored.
15841587
inplace : bool, default True
15851588
Perform inplace modification.
15861589
regex : bool, default False
15871590
If true, perform regular expression substitution.
1588-
mask : array-like of bool, optional
1589-
True indicate corresponding element is ignored.
15901591
15911592
Returns
15921593
-------
@@ -2495,7 +2496,11 @@ def _can_hold_element(self, element: Any) -> bool:
24952496
return True
24962497

24972498
def replace(
2498-
self, to_replace, value, inplace=False, regex=False, convert=True
2499+
self,
2500+
to_replace,
2501+
value,
2502+
inplace: bool = False,
2503+
regex: bool = False,
24992504
) -> List["Block"]:
25002505
to_rep_is_list = is_list_like(to_replace)
25012506
value_is_list = is_list_like(value)
@@ -2506,20 +2511,14 @@ def replace(
25062511
blocks: List["Block"] = [self]
25072512

25082513
if not either_list and is_re(to_replace):
2509-
return self._replace_single(
2510-
to_replace, value, inplace=inplace, regex=True, convert=convert
2511-
)
2514+
return self._replace_single(to_replace, value, inplace=inplace, regex=True)
25122515
elif not (either_list or regex):
2513-
return super().replace(
2514-
to_replace, value, inplace=inplace, regex=regex, convert=convert
2515-
)
2516+
return super().replace(to_replace, value, inplace=inplace, regex=regex)
25162517
elif both_lists:
25172518
for to_rep, v in zip(to_replace, value):
25182519
result_blocks = []
25192520
for b in blocks:
2520-
result = b._replace_single(
2521-
to_rep, v, inplace=inplace, regex=regex, convert=convert
2522-
)
2521+
result = b._replace_single(to_rep, v, inplace=inplace, regex=regex)
25232522
result_blocks.extend(result)
25242523
blocks = result_blocks
25252524
return result_blocks
@@ -2529,18 +2528,22 @@ def replace(
25292528
result_blocks = []
25302529
for b in blocks:
25312530
result = b._replace_single(
2532-
to_rep, value, inplace=inplace, regex=regex, convert=convert
2531+
to_rep, value, inplace=inplace, regex=regex
25332532
)
25342533
result_blocks.extend(result)
25352534
blocks = result_blocks
25362535
return result_blocks
25372536

2538-
return self._replace_single(
2539-
to_replace, value, inplace=inplace, convert=convert, regex=regex
2540-
)
2537+
return self._replace_single(to_replace, value, inplace=inplace, regex=regex)
25412538

25422539
def _replace_single(
2543-
self, to_replace, value, inplace=False, regex=False, convert=True, mask=None
2540+
self,
2541+
to_replace,
2542+
value,
2543+
inplace: bool = False,
2544+
regex: bool = False,
2545+
convert: bool = True,
2546+
mask=None,
25442547
) -> List["Block"]:
25452548
"""
25462549
Replace elements by the given value.
@@ -2567,23 +2570,7 @@ def _replace_single(
25672570
inplace = validate_bool_kwarg(inplace, "inplace")
25682571

25692572
# to_replace is regex compilable
2570-
to_rep_re = regex and is_re_compilable(to_replace)
2571-
2572-
# regex is regex compilable
2573-
regex_re = is_re_compilable(regex)
2574-
2575-
# only one will survive
2576-
if to_rep_re and regex_re:
2577-
raise AssertionError(
2578-
"only one of to_replace and regex can be regex compilable"
2579-
)
2580-
2581-
# if regex was passed as something that can be a regex (rather than a
2582-
# boolean)
2583-
if regex_re:
2584-
to_replace = regex
2585-
2586-
regex = regex_re or to_rep_re
2573+
regex = regex and is_re_compilable(to_replace)
25872574

25882575
# try to get the pattern attribute (compiled re) or it's a string
25892576
if is_re(to_replace):
@@ -2646,7 +2633,6 @@ def replace(
26462633
value,
26472634
inplace: bool = False,
26482635
regex: bool = False,
2649-
convert: bool = True,
26502636
) -> List["Block"]:
26512637
inplace = validate_bool_kwarg(inplace, "inplace")
26522638
result = self if inplace else self.copy()

pandas/core/internals/managers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,11 @@ def convert(
638638
coerce=coerce,
639639
)
640640

641-
def replace(self, value, **kwargs) -> "BlockManager":
641+
def replace(self, to_replace, value, inplace: bool, regex: bool) -> "BlockManager":
642642
assert np.ndim(value) == 0, value
643-
return self.apply("replace", value=value, **kwargs)
643+
return self.apply(
644+
"replace", to_replace=to_replace, value=value, inplace=inplace, regex=regex
645+
)
644646

645647
def replace_list(
646648
self: T,

0 commit comments

Comments
 (0)