39
39
import numpy as np
40
40
from numpy import ma
41
41
42
- from pandas ._config import (
43
- get_option ,
44
- using_copy_on_write ,
45
- )
42
+ from pandas ._config import get_option
46
43
47
44
from pandas ._libs import (
48
45
algos as libalgos ,
62
59
from pandas .errors .cow import (
63
60
_chained_assignment_method_msg ,
64
61
_chained_assignment_msg ,
65
- _chained_assignment_warning_method_msg ,
66
62
)
67
63
from pandas .util ._decorators import (
68
64
Appender ,
@@ -707,8 +703,7 @@ def __init__(
707
703
stacklevel = 1 , # bump to 2 once pyarrow 15.0 is released with fix
708
704
)
709
705
710
- if using_copy_on_write ():
711
- data = data .copy (deep = False )
706
+ data = data .copy (deep = False )
712
707
# first check if a Manager is passed without any other arguments
713
708
# -> use fastpath (without checking Manager type)
714
709
if index is None and columns is None and dtype is None and not copy :
@@ -730,9 +725,7 @@ def __init__(
730
725
if isinstance (data , dict ):
731
726
# retain pre-GH#38939 default behavior
732
727
copy = True
733
- elif using_copy_on_write () and not isinstance (
734
- data , (Index , DataFrame , Series )
735
- ):
728
+ elif not isinstance (data , (Index , DataFrame , Series )):
736
729
copy = True
737
730
else :
738
731
copy = False
@@ -785,15 +778,14 @@ def __init__(
785
778
)
786
779
elif getattr (data , "name" , None ) is not None :
787
780
# i.e. Series/Index with non-None name
788
- _copy = copy if using_copy_on_write () else True
789
781
mgr = dict_to_mgr (
790
782
# error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
791
783
# attribute "name"
792
784
{data .name : data }, # type: ignore[union-attr]
793
785
index ,
794
786
columns ,
795
787
dtype = dtype ,
796
- copy = _copy ,
788
+ copy = copy ,
797
789
)
798
790
else :
799
791
mgr = ndarray_to_mgr (
@@ -1500,10 +1492,9 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
1500
1492
"""
1501
1493
columns = self .columns
1502
1494
klass = self ._constructor_sliced
1503
- using_cow = using_copy_on_write ()
1504
1495
for k , v in zip (self .index , self .values ):
1505
1496
s = klass (v , index = columns , name = k ).__finalize__ (self )
1506
- if using_cow and self ._mgr .is_single_block :
1497
+ if self ._mgr .is_single_block :
1507
1498
s ._mgr .add_references (self ._mgr )
1508
1499
yield k , s
1509
1500
@@ -3669,8 +3660,6 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
3669
3660
if self ._can_fast_transpose :
3670
3661
# Note: tests pass without this, but this improves perf quite a bit.
3671
3662
new_vals = self ._values .T
3672
- if copy and not using_copy_on_write ():
3673
- new_vals = new_vals .copy ()
3674
3663
3675
3664
result = self ._constructor (
3676
3665
new_vals ,
@@ -3679,7 +3668,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
3679
3668
copy = False ,
3680
3669
dtype = new_vals .dtype ,
3681
3670
)
3682
- if using_copy_on_write () and len (self ) > 0 :
3671
+ if len (self ) > 0 :
3683
3672
result ._mgr .add_references (self ._mgr )
3684
3673
3685
3674
elif (
@@ -3723,8 +3712,6 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
3723
3712
3724
3713
else :
3725
3714
new_arr = self .values .T
3726
- if copy and not using_copy_on_write ():
3727
- new_arr = new_arr .copy ()
3728
3715
result = self ._constructor (
3729
3716
new_arr ,
3730
3717
index = self .columns ,
@@ -4043,7 +4030,7 @@ def isetitem(self, loc, value) -> None:
4043
4030
self ._iset_item_mgr (loc , arraylike , inplace = False , refs = refs )
4044
4031
4045
4032
def __setitem__ (self , key , value ) -> None :
4046
- if not PYPY and using_copy_on_write () :
4033
+ if not PYPY :
4047
4034
if sys .getrefcount (self ) <= 3 :
4048
4035
warnings .warn (
4049
4036
_chained_assignment_msg , ChainedAssignmentError , stacklevel = 2
@@ -4251,12 +4238,7 @@ def _set_item_mgr(
4251
4238
def _iset_item (self , loc : int , value : Series , inplace : bool = True ) -> None :
4252
4239
# We are only called from _replace_columnwise which guarantees that
4253
4240
# no reindex is necessary
4254
- if using_copy_on_write ():
4255
- self ._iset_item_mgr (
4256
- loc , value ._values , inplace = inplace , refs = value ._references
4257
- )
4258
- else :
4259
- self ._iset_item_mgr (loc , value ._values .copy (), inplace = True )
4241
+ self ._iset_item_mgr (loc , value ._values , inplace = inplace , refs = value ._references )
4260
4242
4261
4243
def _set_item (self , key , value ) -> None :
4262
4244
"""
@@ -4992,9 +4974,7 @@ def _series(self):
4992
4974
# ----------------------------------------------------------------------
4993
4975
# Reindexing and alignment
4994
4976
4995
- def _reindex_multi (
4996
- self , axes : dict [str , Index ], copy : bool , fill_value
4997
- ) -> DataFrame :
4977
+ def _reindex_multi (self , axes : dict [str , Index ], fill_value ) -> DataFrame :
4998
4978
"""
4999
4979
We are guaranteed non-Nones in the axes.
5000
4980
"""
@@ -5016,7 +4996,7 @@ def _reindex_multi(
5016
4996
else :
5017
4997
return self ._reindex_with_indexers (
5018
4998
{0 : [new_index , row_indexer ], 1 : [new_columns , col_indexer ]},
5019
- copy = copy ,
4999
+ copy = False ,
5020
5000
fill_value = fill_value ,
5021
5001
)
5022
5002
@@ -6937,7 +6917,7 @@ def sort_values(
6937
6917
return self .copy (deep = None )
6938
6918
6939
6919
if is_range_indexer (indexer , len (indexer )):
6940
- result = self .copy (deep = ( not inplace and not using_copy_on_write ()) )
6920
+ result = self .copy (deep = False )
6941
6921
if ignore_index :
6942
6922
result .index = default_index (len (result ))
6943
6923
@@ -8745,20 +8725,13 @@ def update(
8745
8725
1 2 500.0
8746
8726
2 3 6.0
8747
8727
"""
8748
- if not PYPY and using_copy_on_write () :
8728
+ if not PYPY :
8749
8729
if sys .getrefcount (self ) <= REF_COUNT :
8750
8730
warnings .warn (
8751
8731
_chained_assignment_method_msg ,
8752
8732
ChainedAssignmentError ,
8753
8733
stacklevel = 2 ,
8754
8734
)
8755
- elif not PYPY and not using_copy_on_write () and self ._is_view_after_cow_rules ():
8756
- if sys .getrefcount (self ) <= REF_COUNT :
8757
- warnings .warn (
8758
- _chained_assignment_warning_method_msg ,
8759
- FutureWarning ,
8760
- stacklevel = 2 ,
8761
- )
8762
8735
8763
8736
# TODO: Support other joins
8764
8737
if join != "left" : # pragma: no cover
@@ -12053,7 +12026,7 @@ def to_timestamp(
12053
12026
>>> df2.index
12054
12027
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
12055
12028
"""
12056
- new_obj = self .copy (deep = copy and not using_copy_on_write () )
12029
+ new_obj = self .copy (deep = False )
12057
12030
12058
12031
axis_name = self ._get_axis_name (axis )
12059
12032
old_ax = getattr (self , axis_name )
@@ -12122,7 +12095,7 @@ def to_period(
12122
12095
>>> idx.to_period("Y")
12123
12096
PeriodIndex(['2001', '2002', '2003'], dtype='period[Y-DEC]')
12124
12097
"""
12125
- new_obj = self .copy (deep = copy and not using_copy_on_write () )
12098
+ new_obj = self .copy (deep = False )
12126
12099
12127
12100
axis_name = self ._get_axis_name (axis )
12128
12101
old_ax = getattr (self , axis_name )
@@ -12445,7 +12418,7 @@ def _reindex_for_setitem(
12445
12418
# reindex if necessary
12446
12419
12447
12420
if value .index .equals (index ) or not len (index ):
12448
- if using_copy_on_write () and isinstance (value , Series ):
12421
+ if isinstance (value , Series ):
12449
12422
return value ._values , value ._references
12450
12423
return value ._values .copy (), None
12451
12424
0 commit comments