@@ -717,7 +717,7 @@ def replace(
717
717
inplace : bool = False ,
718
718
regex : bool = False ,
719
719
convert : bool = True ,
720
- ):
720
+ ) -> List [ "Block" ] :
721
721
"""
722
722
replace the to_replace value with value, possible to create new
723
723
blocks here this is just a call to putmask. regex is not used here.
@@ -808,9 +808,11 @@ def replace(
808
808
)
809
809
return blocks
810
810
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" ]:
812
814
""" no-op on a non-ObjectBlock """
813
- return self if kwargs [ " inplace" ] else self .copy ()
815
+ return [ self ] if inplace else [ self .copy ()]
814
816
815
817
def _replace_list (
816
818
self ,
@@ -852,16 +854,13 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
852
854
to_replace = src ,
853
855
value = dest ,
854
856
inplace = inplace ,
855
- convert = convert ,
856
857
regex = regex ,
857
858
)
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 )
865
864
rb = new_rb
866
865
return rb
867
866
@@ -1559,9 +1558,8 @@ def _replace_coerce(
1559
1558
value ,
1560
1559
inplace : bool = True ,
1561
1560
regex : bool = False ,
1562
- convert : bool = False ,
1563
1561
mask = None ,
1564
- ):
1562
+ ) -> List [ "Block" ] :
1565
1563
"""
1566
1564
Replace value corresponding to the given boolean array with another
1567
1565
value.
@@ -1576,14 +1574,12 @@ def _replace_coerce(
1576
1574
Perform inplace modification.
1577
1575
regex : bool, default False
1578
1576
If true, perform regular expression substitution.
1579
- convert : bool, default True
1580
- If true, try to coerce any object types to better types.
1581
1577
mask : array-like of bool, optional
1582
1578
True indicate corresponding element is ignored.
1583
1579
1584
1580
Returns
1585
1581
-------
1586
- A new block if there is anything to replace or the original block.
1582
+ List[Block]
1587
1583
"""
1588
1584
if mask .any ():
1589
1585
if not regex :
@@ -1595,10 +1591,10 @@ def _replace_coerce(
1595
1591
value ,
1596
1592
inplace = inplace ,
1597
1593
regex = regex ,
1598
- convert = convert ,
1594
+ convert = False ,
1599
1595
mask = mask ,
1600
1596
)
1601
- return self
1597
+ return [ self ]
1602
1598
1603
1599
1604
1600
class ExtensionBlock (Block ):
@@ -2479,14 +2475,16 @@ def _maybe_downcast(self, blocks: List["Block"], downcast=None) -> List["Block"]
2479
2475
def _can_hold_element (self , element : Any ) -> bool :
2480
2476
return True
2481
2477
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" ]:
2483
2481
to_rep_is_list = is_list_like (to_replace )
2484
2482
value_is_list = is_list_like (value )
2485
2483
both_lists = to_rep_is_list and value_is_list
2486
2484
either_list = to_rep_is_list or value_is_list
2487
2485
2488
- result_blocks = []
2489
- blocks = [self ]
2486
+ result_blocks : List [ "Block" ] = []
2487
+ blocks : List [ "Block" ] = [self ]
2490
2488
2491
2489
if not either_list and is_re (to_replace ):
2492
2490
return self ._replace_single (
@@ -2503,7 +2501,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
2503
2501
result = b ._replace_single (
2504
2502
to_rep , v , inplace = inplace , regex = regex , convert = convert
2505
2503
)
2506
- result_blocks = extend_blocks (result , result_blocks )
2504
+ result_blocks . extend (result )
2507
2505
blocks = result_blocks
2508
2506
return result_blocks
2509
2507
@@ -2514,7 +2512,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
2514
2512
result = b ._replace_single (
2515
2513
to_rep , value , inplace = inplace , regex = regex , convert = convert
2516
2514
)
2517
- result_blocks = extend_blocks (result , result_blocks )
2515
+ result_blocks . extend (result )
2518
2516
blocks = result_blocks
2519
2517
return result_blocks
2520
2518
@@ -2524,7 +2522,7 @@ def replace(self, to_replace, value, inplace=False, regex=False, convert=True):
2524
2522
2525
2523
def _replace_single (
2526
2524
self , to_replace , value , inplace = False , regex = False , convert = True , mask = None
2527
- ):
2525
+ ) -> List [ "Block" ] :
2528
2526
"""
2529
2527
Replace elements by the given value.
2530
2528
@@ -2545,7 +2543,7 @@ def _replace_single(
2545
2543
2546
2544
Returns
2547
2545
-------
2548
- a new block, the result after replacing
2546
+ List[Block]
2549
2547
"""
2550
2548
inplace = validate_bool_kwarg (inplace , "inplace" )
2551
2549
@@ -2619,48 +2617,6 @@ def re_replacer(s):
2619
2617
nbs = [block ]
2620
2618
return nbs
2621
2619
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
-
2664
2620
2665
2621
class CategoricalBlock (ExtensionBlock ):
2666
2622
__slots__ = ()
@@ -2672,12 +2628,12 @@ def replace(
2672
2628
inplace : bool = False ,
2673
2629
regex : bool = False ,
2674
2630
convert : bool = True ,
2675
- ):
2631
+ ) -> List [ "Block" ] :
2676
2632
inplace = validate_bool_kwarg (inplace , "inplace" )
2677
2633
result = self if inplace else self .copy ()
2678
2634
2679
2635
result .values .replace (to_replace , value , inplace = True )
2680
- return result
2636
+ return [ result ]
2681
2637
2682
2638
2683
2639
# -----------------------------------------------------------------
0 commit comments