@@ -66,6 +66,7 @@ def test_copy_shallow(using_copy_on_write):
6666 lambda df , copy : df .rename (columns = str .lower , copy = copy ),
6767 lambda df , copy : df .reindex (columns = ["a" , "c" ], copy = copy ),
6868 lambda df , copy : df .reindex_like (df , copy = copy ),
69+ lambda df , copy : df .align (df , copy = copy )[0 ],
6970 lambda df , copy : df .set_axis (["a" , "b" , "c" ], axis = "index" , copy = copy ),
7071 lambda df , copy : df .rename_axis (index = "test" , copy = copy ),
7172 lambda df , copy : df .rename_axis (columns = "test" , copy = copy ),
@@ -84,6 +85,7 @@ def test_copy_shallow(using_copy_on_write):
8485 "rename" ,
8586 "reindex" ,
8687 "reindex_like" ,
88+ "align" ,
8789 "set_axis" ,
8890 "rename_axis0" ,
8991 "rename_axis1" ,
@@ -115,22 +117,96 @@ def test_methods_copy_keyword(
115117 df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]}, index = index )
116118 df2 = method (df , copy = copy )
117119
118- share_memory = ( using_copy_on_write and copy is not True ) or copy is False
120+ share_memory = using_copy_on_write or copy is False
119121
120122 if request .node .callspec .id .startswith ("reindex-" ):
121123 # TODO copy=False without CoW still returns a copy in this case
122124 if not using_copy_on_write and not using_array_manager and copy is False :
123125 share_memory = False
124- # TODO copy=True with CoW still returns a view
125- if using_copy_on_write :
126- share_memory = True
127126
128127 if share_memory :
129128 assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
130129 else :
131130 assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
132131
133132
133+ @pytest .mark .parametrize ("copy" , [True , None , False ])
134+ @pytest .mark .parametrize (
135+ "method" ,
136+ [
137+ lambda ser , copy : ser .rename (index = {0 : 100 }, copy = copy ),
138+ lambda ser , copy : ser .reindex (index = ser .index , copy = copy ),
139+ lambda ser , copy : ser .reindex_like (ser , copy = copy ),
140+ lambda ser , copy : ser .align (ser , copy = copy )[0 ],
141+ lambda ser , copy : ser .set_axis (["a" , "b" , "c" ], axis = "index" , copy = copy ),
142+ lambda ser , copy : ser .rename_axis (index = "test" , copy = copy ),
143+ lambda ser , copy : ser .astype ("int64" , copy = copy ),
144+ lambda ser , copy : ser .swaplevel (0 , 1 , copy = copy ),
145+ lambda ser , copy : ser .swapaxes (0 , 0 , copy = copy ),
146+ lambda ser , copy : ser .truncate (0 , 5 , copy = copy ),
147+ lambda ser , copy : ser .infer_objects (copy = copy ),
148+ lambda ser , copy : ser .to_timestamp (copy = copy ),
149+ lambda ser , copy : ser .to_period (freq = "D" , copy = copy ),
150+ lambda ser , copy : ser .tz_localize ("US/Central" , copy = copy ),
151+ lambda ser , copy : ser .tz_convert ("US/Central" , copy = copy ),
152+ lambda ser , copy : ser .set_flags (allows_duplicate_labels = False , copy = copy ),
153+ ],
154+ ids = [
155+ "rename" ,
156+ "reindex" ,
157+ "reindex_like" ,
158+ "align" ,
159+ "set_axis" ,
160+ "rename_axis0" ,
161+ "astype" ,
162+ "swaplevel" ,
163+ "swapaxes" ,
164+ "truncate" ,
165+ "infer_objects" ,
166+ "to_timestamp" ,
167+ "to_period" ,
168+ "tz_localize" ,
169+ "tz_convert" ,
170+ "set_flags" ,
171+ ],
172+ )
173+ def test_methods_series_copy_keyword (request , method , copy , using_copy_on_write ):
174+ index = None
175+ if "to_timestamp" in request .node .callspec .id :
176+ index = period_range ("2012-01-01" , freq = "D" , periods = 3 )
177+ elif "to_period" in request .node .callspec .id :
178+ index = date_range ("2012-01-01" , freq = "D" , periods = 3 )
179+ elif "tz_localize" in request .node .callspec .id :
180+ index = date_range ("2012-01-01" , freq = "D" , periods = 3 )
181+ elif "tz_convert" in request .node .callspec .id :
182+ index = date_range ("2012-01-01" , freq = "D" , periods = 3 , tz = "Europe/Brussels" )
183+ elif "swaplevel" in request .node .callspec .id :
184+ index = MultiIndex .from_arrays ([[1 , 2 , 3 ], [4 , 5 , 6 ]])
185+
186+ ser = Series ([1 , 2 , 3 ], index = index )
187+ ser2 = method (ser , copy = copy )
188+
189+ share_memory = using_copy_on_write or copy is False
190+
191+ if share_memory :
192+ assert np .shares_memory (get_array (ser2 ), get_array (ser ))
193+ else :
194+ assert not np .shares_memory (get_array (ser2 ), get_array (ser ))
195+
196+
197+ @pytest .mark .parametrize ("copy" , [True , None , False ])
198+ def test_transpose_copy_keyword (using_copy_on_write , copy , using_array_manager ):
199+ df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
200+ result = df .transpose (copy = copy )
201+ share_memory = using_copy_on_write or copy is False or copy is None
202+ share_memory = share_memory and not using_array_manager
203+
204+ if share_memory :
205+ assert np .shares_memory (get_array (df , "a" ), get_array (result , 0 ))
206+ else :
207+ assert not np .shares_memory (get_array (df , "a" ), get_array (result , 0 ))
208+
209+
134210# -----------------------------------------------------------------------------
135211# DataFrame methods returning new DataFrame using shallow copy
136212
@@ -1119,14 +1195,13 @@ def test_set_flags(using_copy_on_write):
11191195 tm .assert_series_equal (ser , expected )
11201196
11211197
1122- @pytest .mark .parametrize ("copy_kwargs" , [{"copy" : True }, {}])
11231198@pytest .mark .parametrize ("kwargs" , [{"mapper" : "test" }, {"index" : "test" }])
1124- def test_rename_axis (using_copy_on_write , kwargs , copy_kwargs ):
1199+ def test_rename_axis (using_copy_on_write , kwargs ):
11251200 df = DataFrame ({"a" : [1 , 2 , 3 , 4 ]}, index = Index ([1 , 2 , 3 , 4 ], name = "a" ))
11261201 df_orig = df .copy ()
1127- df2 = df .rename_axis (** kwargs , ** copy_kwargs )
1202+ df2 = df .rename_axis (** kwargs )
11281203
1129- if using_copy_on_write and not copy_kwargs :
1204+ if using_copy_on_write :
11301205 assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
11311206 else :
11321207 assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
0 commit comments