@@ -720,6 +720,9 @@ def copy(self, deep: bool = True):
720
720
values = values .copy ()
721
721
return self .make_block_same_class (values , ndim = self .ndim )
722
722
723
+ # ---------------------------------------------------------------------
724
+ # Replace
725
+
723
726
def replace (
724
727
self ,
725
728
to_replace ,
@@ -870,6 +873,57 @@ def _replace_list(
870
873
rb = new_rb
871
874
return rb
872
875
876
+ def _replace_coerce (
877
+ self ,
878
+ to_replace ,
879
+ value ,
880
+ mask : np .ndarray ,
881
+ inplace : bool = True ,
882
+ regex : bool = False ,
883
+ ) -> List [Block ]:
884
+ """
885
+ Replace value corresponding to the given boolean array with another
886
+ value.
887
+
888
+ Parameters
889
+ ----------
890
+ to_replace : object or pattern
891
+ Scalar to replace or regular expression to match.
892
+ value : object
893
+ Replacement object.
894
+ mask : np.ndarray[bool]
895
+ True indicate corresponding element is ignored.
896
+ inplace : bool, default True
897
+ Perform inplace modification.
898
+ regex : bool, default False
899
+ If true, perform regular expression substitution.
900
+
901
+ Returns
902
+ -------
903
+ List[Block]
904
+ """
905
+ if mask .any ():
906
+ if not regex :
907
+ nb = self .coerce_to_target_dtype (value )
908
+ if nb is self and not inplace :
909
+ nb = nb .copy ()
910
+ putmask_inplace (nb .values , mask , value )
911
+ return [nb ]
912
+ else :
913
+ regex = should_use_regex (regex , to_replace )
914
+ if regex :
915
+ return self ._replace_regex (
916
+ to_replace ,
917
+ value ,
918
+ inplace = inplace ,
919
+ convert = False ,
920
+ mask = mask ,
921
+ )
922
+ return self .replace (to_replace , value , inplace = inplace , regex = False )
923
+ return [self ]
924
+
925
+ # ---------------------------------------------------------------------
926
+
873
927
def setitem (self , indexer , value ):
874
928
"""
875
929
Attempt self.values[indexer] = value, possibly creating a new array.
@@ -1400,55 +1454,6 @@ def quantile(
1400
1454
1401
1455
return make_block (result , placement = self .mgr_locs , ndim = 2 )
1402
1456
1403
- def _replace_coerce (
1404
- self ,
1405
- to_replace ,
1406
- value ,
1407
- mask : np .ndarray ,
1408
- inplace : bool = True ,
1409
- regex : bool = False ,
1410
- ) -> List [Block ]:
1411
- """
1412
- Replace value corresponding to the given boolean array with another
1413
- value.
1414
-
1415
- Parameters
1416
- ----------
1417
- to_replace : object or pattern
1418
- Scalar to replace or regular expression to match.
1419
- value : object
1420
- Replacement object.
1421
- mask : np.ndarray[bool]
1422
- True indicate corresponding element is ignored.
1423
- inplace : bool, default True
1424
- Perform inplace modification.
1425
- regex : bool, default False
1426
- If true, perform regular expression substitution.
1427
-
1428
- Returns
1429
- -------
1430
- List[Block]
1431
- """
1432
- if mask .any ():
1433
- if not regex :
1434
- nb = self .coerce_to_target_dtype (value )
1435
- if nb is self and not inplace :
1436
- nb = nb .copy ()
1437
- putmask_inplace (nb .values , mask , value )
1438
- return [nb ]
1439
- else :
1440
- regex = should_use_regex (regex , to_replace )
1441
- if regex :
1442
- return self ._replace_regex (
1443
- to_replace ,
1444
- value ,
1445
- inplace = inplace ,
1446
- convert = False ,
1447
- mask = mask ,
1448
- )
1449
- return self .replace (to_replace , value , inplace = inplace , regex = False )
1450
- return [self ]
1451
-
1452
1457
1453
1458
class ExtensionBlock (Block ):
1454
1459
"""
0 commit comments