@@ -726,7 +726,6 @@ def replace(
726
726
value ,
727
727
inplace : bool = False ,
728
728
regex : bool = False ,
729
- convert : bool = True ,
730
729
) -> List ["Block" ]:
731
730
"""
732
731
replace the to_replace value with value, possible to create new
@@ -755,9 +754,7 @@ def replace(
755
754
if len (to_replace ) == 1 :
756
755
# _can_hold_element checks have reduced this back to the
757
756
# 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 )
761
758
762
759
# GH 22083, TypeError or ValueError occurred within error handling
763
760
# causes infinite loop. Cast and retry only if not objectblock.
@@ -771,7 +768,6 @@ def replace(
771
768
value = value ,
772
769
inplace = inplace ,
773
770
regex = regex ,
774
- convert = convert ,
775
771
)
776
772
777
773
values = self .values
@@ -810,16 +806,21 @@ def replace(
810
806
value = value ,
811
807
inplace = inplace ,
812
808
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 ]
818
809
)
810
+
811
+ blocks = extend_blocks (
812
+ [b .convert (numeric = False , copy = not inplace ) for b in blocks ]
813
+ )
819
814
return blocks
820
815
821
816
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 ,
823
824
) -> List ["Block" ]:
824
825
""" no-op on a non-ObjectBlock """
825
826
return [self ] if inplace else [self .copy ()]
@@ -860,9 +861,9 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
860
861
m = masks [i ]
861
862
convert = i == src_len # only convert once at the end
862
863
result = blk ._replace_coerce (
863
- mask = m ,
864
864
to_replace = src ,
865
865
value = dest ,
866
+ mask = m ,
866
867
inplace = inplace ,
867
868
regex = regex ,
868
869
)
@@ -1567,9 +1568,9 @@ def _replace_coerce(
1567
1568
self ,
1568
1569
to_replace ,
1569
1570
value ,
1571
+ mask : np .ndarray ,
1570
1572
inplace : bool = True ,
1571
1573
regex : bool = False ,
1572
- mask = None ,
1573
1574
) -> List ["Block" ]:
1574
1575
"""
1575
1576
Replace value corresponding to the given boolean array with another
@@ -1581,12 +1582,12 @@ def _replace_coerce(
1581
1582
Scalar to replace or regular expression to match.
1582
1583
value : object
1583
1584
Replacement object.
1585
+ mask : np.ndarray[bool]
1586
+ True indicate corresponding element is ignored.
1584
1587
inplace : bool, default True
1585
1588
Perform inplace modification.
1586
1589
regex : bool, default False
1587
1590
If true, perform regular expression substitution.
1588
- mask : array-like of bool, optional
1589
- True indicate corresponding element is ignored.
1590
1591
1591
1592
Returns
1592
1593
-------
@@ -2495,7 +2496,11 @@ def _can_hold_element(self, element: Any) -> bool:
2495
2496
return True
2496
2497
2497
2498
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 ,
2499
2504
) -> List ["Block" ]:
2500
2505
to_rep_is_list = is_list_like (to_replace )
2501
2506
value_is_list = is_list_like (value )
@@ -2506,20 +2511,14 @@ def replace(
2506
2511
blocks : List ["Block" ] = [self ]
2507
2512
2508
2513
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 )
2512
2515
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 )
2516
2517
elif both_lists :
2517
2518
for to_rep , v in zip (to_replace , value ):
2518
2519
result_blocks = []
2519
2520
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 )
2523
2522
result_blocks .extend (result )
2524
2523
blocks = result_blocks
2525
2524
return result_blocks
@@ -2529,18 +2528,22 @@ def replace(
2529
2528
result_blocks = []
2530
2529
for b in blocks :
2531
2530
result = b ._replace_single (
2532
- to_rep , value , inplace = inplace , regex = regex , convert = convert
2531
+ to_rep , value , inplace = inplace , regex = regex
2533
2532
)
2534
2533
result_blocks .extend (result )
2535
2534
blocks = result_blocks
2536
2535
return result_blocks
2537
2536
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 )
2541
2538
2542
2539
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 ,
2544
2547
) -> List ["Block" ]:
2545
2548
"""
2546
2549
Replace elements by the given value.
@@ -2567,23 +2570,7 @@ def _replace_single(
2567
2570
inplace = validate_bool_kwarg (inplace , "inplace" )
2568
2571
2569
2572
# 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 )
2587
2574
2588
2575
# try to get the pattern attribute (compiled re) or it's a string
2589
2576
if is_re (to_replace ):
@@ -2646,7 +2633,6 @@ def replace(
2646
2633
value ,
2647
2634
inplace : bool = False ,
2648
2635
regex : bool = False ,
2649
- convert : bool = True ,
2650
2636
) -> List ["Block" ]:
2651
2637
inplace = validate_bool_kwarg (inplace , "inplace" )
2652
2638
result = self if inplace else self .copy ()
0 commit comments