@@ -549,9 +549,7 @@ def _maybe_downcast(
549
549
if caller == "fillna" and get_option ("future.no_silent_downcasting" ):
550
550
return blocks
551
551
552
- nbs = extend_blocks (
553
- [blk .convert (using_cow = True , copy = False ) for blk in blocks ]
554
- )
552
+ nbs = extend_blocks ([blk .convert () for blk in blocks ])
555
553
if caller == "fillna" :
556
554
if len (nbs ) != len (blocks ) or not all (
557
555
x .dtype == y .dtype for x , y in zip (nbs , blocks )
@@ -575,7 +573,7 @@ def _maybe_downcast(
575
573
elif caller == "where" and get_option ("future.no_silent_downcasting" ) is True :
576
574
return blocks
577
575
else :
578
- nbs = extend_blocks ([b ._downcast_2d (downcast , True ) for b in blocks ])
576
+ nbs = extend_blocks ([b ._downcast_2d (downcast ) for b in blocks ])
579
577
580
578
# When _maybe_downcast is called with caller="where", it is either
581
579
# a) with downcast=False, which is a no-op (the desired future behavior)
@@ -606,7 +604,7 @@ def _maybe_downcast(
606
604
607
605
@final
608
606
@maybe_split
609
- def _downcast_2d (self , dtype , using_cow : bool = False ) -> list [Block ]:
607
+ def _downcast_2d (self , dtype ) -> list [Block ]:
610
608
"""
611
609
downcast specialized to 2D case post-validation.
612
610
@@ -618,30 +616,19 @@ def _downcast_2d(self, dtype, using_cow: bool = False) -> list[Block]:
618
616
return [self .make_block (new_values , refs = refs )]
619
617
620
618
@final
621
- def convert (
622
- self ,
623
- * ,
624
- copy : bool = True ,
625
- using_cow : bool = False ,
626
- ) -> list [Block ]:
619
+ def convert (self ) -> list [Block ]:
627
620
"""
628
621
Attempt to coerce any object types to better types. Return a copy
629
622
of the block (if copy = True).
630
623
"""
631
624
if not self .is_object :
632
- if not copy and using_cow :
633
- return [self .copy (deep = False )]
634
- return [self .copy ()] if copy else [self ]
625
+ return [self .copy (deep = False )]
635
626
636
627
if self .ndim != 1 and self .shape [0 ] != 1 :
637
- blocks = self .split_and_operate (
638
- Block .convert , copy = copy , using_cow = using_cow
639
- )
628
+ blocks = self .split_and_operate (Block .convert )
640
629
if all (blk .dtype .kind == "O" for blk in blocks ):
641
630
# Avoid fragmenting the block if convert is a no-op
642
- if using_cow :
643
- return [self .copy (deep = False )]
644
- return [self .copy ()] if copy else [self ]
631
+ return [self .copy (deep = False )]
645
632
return blocks
646
633
647
634
values = self .values
@@ -655,9 +642,7 @@ def convert(
655
642
convert_non_numeric = True ,
656
643
)
657
644
refs = None
658
- if copy and res_values is values :
659
- res_values = values .copy ()
660
- elif res_values is values :
645
+ if res_values is values :
661
646
refs = self .refs
662
647
663
648
res_values = ensure_block_shape (res_values , self .ndim )
@@ -674,7 +659,7 @@ def convert_dtypes(
674
659
dtype_backend : DtypeBackend = "numpy_nullable" ,
675
660
) -> list [Block ]:
676
661
if infer_objects and self .is_object :
677
- blks = self .convert (copy = False )
662
+ blks = self .convert ()
678
663
else :
679
664
blks = [self ]
680
665
@@ -798,17 +783,6 @@ def _maybe_copy(self, inplace: bool) -> Self:
798
783
return self .copy (deep = deep )
799
784
return self .copy ()
800
785
801
- @final
802
- def _maybe_copy_cow_check (
803
- self , using_cow : bool = True , inplace : bool = True
804
- ) -> Self :
805
- if using_cow and inplace :
806
- deep = self .refs .has_reference ()
807
- blk = self .copy (deep = deep )
808
- else :
809
- blk = self if inplace else self .copy ()
810
- return blk
811
-
812
786
@final
813
787
def _get_refs_and_copy (self , inplace : bool ):
814
788
refs = None
@@ -820,17 +794,6 @@ def _get_refs_and_copy(self, inplace: bool):
820
794
refs = self .refs
821
795
return copy , refs
822
796
823
- @final
824
- def _get_refs_and_copy_cow_check (self , using_cow : bool , inplace : bool ):
825
- refs = None
826
- copy = not inplace
827
- if inplace :
828
- if using_cow and self .refs .has_reference ():
829
- copy = True
830
- else :
831
- refs = self .refs
832
- return copy , refs
833
-
834
797
# ---------------------------------------------------------------------
835
798
# Replace
836
799
@@ -842,7 +805,6 @@ def replace(
842
805
inplace : bool = False ,
843
806
# mask may be pre-computed if we're called from replace_list
844
807
mask : npt .NDArray [np .bool_ ] | None = None ,
845
- using_cow : bool = False ,
846
808
) -> list [Block ]:
847
809
"""
848
810
replace the to_replace value with value, possible to create new
@@ -857,7 +819,7 @@ def replace(
857
819
if isinstance (values , Categorical ):
858
820
# TODO: avoid special-casing
859
821
# GH49404
860
- blk = self ._maybe_copy_cow_check ( using_cow , inplace )
822
+ blk = self ._maybe_copy ( inplace )
861
823
values = cast (Categorical , blk .values )
862
824
values ._replace (to_replace = to_replace , value = value , inplace = True )
863
825
return [blk ]
@@ -867,25 +829,19 @@ def replace(
867
829
# replacing it is a no-op.
868
830
# Note: If to_replace were a list, NDFrame.replace would call
869
831
# replace_list instead of replace.
870
- if using_cow :
871
- return [self .copy (deep = False )]
872
- else :
873
- return [self ] if inplace else [self .copy ()]
832
+ return [self .copy (deep = False )]
874
833
875
834
if mask is None :
876
835
mask = missing .mask_missing (values , to_replace )
877
836
if not mask .any ():
878
837
# Note: we get here with test_replace_extension_other incorrectly
879
838
# bc _can_hold_element is incorrect.
880
- if using_cow :
881
- return [self .copy (deep = False )]
882
- else :
883
- return [self ] if inplace else [self .copy ()]
839
+ return [self .copy (deep = False )]
884
840
885
841
elif self ._can_hold_element (value ):
886
842
# TODO(CoW): Maybe split here as well into columns where mask has True
887
843
# and rest?
888
- blk = self ._maybe_copy_cow_check ( using_cow , inplace )
844
+ blk = self ._maybe_copy ( inplace )
889
845
putmask_inplace (blk .values , mask , value )
890
846
891
847
if not (self .is_object and value is None ):
@@ -894,7 +850,7 @@ def replace(
894
850
if get_option ("future.no_silent_downcasting" ) is True :
895
851
blocks = [blk ]
896
852
else :
897
- blocks = blk .convert (copy = False , using_cow = using_cow )
853
+ blocks = blk .convert ()
898
854
if len (blocks ) > 1 or blocks [0 ].dtype != blk .dtype :
899
855
warnings .warn (
900
856
# GH#54710
@@ -935,7 +891,6 @@ def replace(
935
891
value = value ,
936
892
inplace = True ,
937
893
mask = mask [i : i + 1 ],
938
- using_cow = using_cow ,
939
894
)
940
895
)
941
896
return blocks
@@ -947,7 +902,6 @@ def _replace_regex(
947
902
value ,
948
903
inplace : bool = False ,
949
904
mask = None ,
950
- using_cow : bool = False ,
951
905
) -> list [Block ]:
952
906
"""
953
907
Replace elements by the given value.
@@ -962,8 +916,6 @@ def _replace_regex(
962
916
Perform inplace modification.
963
917
mask : array-like of bool, optional
964
918
True indicate corresponding element is ignored.
965
- using_cow: bool, default False
966
- Specifying if copy on write is enabled.
967
919
968
920
Returns
969
921
-------
@@ -972,17 +924,15 @@ def _replace_regex(
972
924
if not self ._can_hold_element (to_replace ):
973
925
# i.e. only if self.is_object is True, but could in principle include a
974
926
# String ExtensionBlock
975
- if using_cow :
976
- return [self .copy (deep = False )]
977
- return [self ] if inplace else [self .copy ()]
927
+ return [self .copy (deep = False )]
978
928
979
929
rx = re .compile (to_replace )
980
930
981
- block = self ._maybe_copy_cow_check ( using_cow , inplace )
931
+ block = self ._maybe_copy ( inplace )
982
932
983
933
replace_regex (block .values , rx , value , mask )
984
934
985
- nbs = block .convert (copy = False , using_cow = using_cow )
935
+ nbs = block .convert ()
986
936
opt = get_option ("future.no_silent_downcasting" )
987
937
if (len (nbs ) > 1 or nbs [0 ].dtype != block .dtype ) and not opt :
988
938
warnings .warn (
@@ -1005,7 +955,6 @@ def replace_list(
1005
955
dest_list : Sequence [Any ],
1006
956
inplace : bool = False ,
1007
957
regex : bool = False ,
1008
- using_cow : bool = False ,
1009
958
) -> list [Block ]:
1010
959
"""
1011
960
See BlockManager.replace_list docstring.
@@ -1015,7 +964,7 @@ def replace_list(
1015
964
if isinstance (values , Categorical ):
1016
965
# TODO: avoid special-casing
1017
966
# GH49404
1018
- blk = self ._maybe_copy_cow_check ( using_cow , inplace )
967
+ blk = self ._maybe_copy ( inplace )
1019
968
values = cast (Categorical , blk .values )
1020
969
values ._replace (to_replace = src_list , value = dest_list , inplace = True )
1021
970
return [blk ]
@@ -1025,10 +974,7 @@ def replace_list(
1025
974
(x , y ) for x , y in zip (src_list , dest_list ) if self ._can_hold_element (x )
1026
975
]
1027
976
if not len (pairs ):
1028
- if using_cow :
1029
- return [self .copy (deep = False )]
1030
- # shortcut, nothing to replace
1031
- return [self ] if inplace else [self .copy ()]
977
+ return [self .copy (deep = False )]
1032
978
1033
979
src_len = len (pairs ) - 1
1034
980
@@ -1055,12 +1001,9 @@ def replace_list(
1055
1001
if inplace :
1056
1002
masks = list (masks )
1057
1003
1058
- if using_cow :
1059
- # Don't set up refs here, otherwise we will think that we have
1060
- # references when we check again later
1061
- rb = [self ]
1062
- else :
1063
- rb = [self if inplace else self .copy ()]
1004
+ # Don't set up refs here, otherwise we will think that we have
1005
+ # references when we check again later
1006
+ rb = [self ]
1064
1007
1065
1008
opt = get_option ("future.no_silent_downcasting" )
1066
1009
for i , ((src , dest ), mask ) in enumerate (zip (pairs , masks )):
@@ -1087,10 +1030,9 @@ def replace_list(
1087
1030
mask = m ,
1088
1031
inplace = inplace ,
1089
1032
regex = regex ,
1090
- using_cow = using_cow ,
1091
1033
)
1092
1034
1093
- if using_cow and i != src_len :
1035
+ if i != src_len :
1094
1036
# This is ugly, but we have to get rid of intermediate refs
1095
1037
# that did not go out of scope yet, otherwise we will trigger
1096
1038
# many unnecessary copies
@@ -1109,9 +1051,7 @@ def replace_list(
1109
1051
# GH#44498 avoid unwanted cast-back
1110
1052
nbs = []
1111
1053
for res_blk in result :
1112
- converted = res_blk .convert (
1113
- copy = True and not using_cow , using_cow = using_cow
1114
- )
1054
+ converted = res_blk .convert ()
1115
1055
if len (converted ) > 1 or converted [0 ].dtype != res_blk .dtype :
1116
1056
warnings .warn (
1117
1057
# GH#54710
@@ -1139,7 +1079,6 @@ def _replace_coerce(
1139
1079
mask : npt .NDArray [np .bool_ ],
1140
1080
inplace : bool = True ,
1141
1081
regex : bool = False ,
1142
- using_cow : bool = False ,
1143
1082
) -> list [Block ]:
1144
1083
"""
1145
1084
Replace value corresponding to the given boolean array with another
@@ -1175,22 +1114,19 @@ def _replace_coerce(
1175
1114
if mask .any ():
1176
1115
has_ref = self .refs .has_reference ()
1177
1116
nb = self .astype (np .dtype (object ))
1178
- if ( nb is self or using_cow ) and not inplace :
1117
+ if not inplace :
1179
1118
nb = nb .copy ()
1180
- elif inplace and has_ref and nb .refs .has_reference () and using_cow :
1119
+ elif inplace and has_ref and nb .refs .has_reference ():
1181
1120
# no copy in astype and we had refs before
1182
1121
nb = nb .copy ()
1183
1122
putmask_inplace (nb .values , mask , value )
1184
1123
return [nb ]
1185
- if using_cow :
1186
- return [self ]
1187
- return [self ] if inplace else [self .copy ()]
1124
+ return [self ]
1188
1125
return self .replace (
1189
1126
to_replace = to_replace ,
1190
1127
value = value ,
1191
1128
inplace = inplace ,
1192
1129
mask = mask ,
1193
- using_cow = using_cow ,
1194
1130
)
1195
1131
1196
1132
# ---------------------------------------------------------------------
0 commit comments