@@ -66,6 +66,7 @@ def test_copy_shallow(using_copy_on_write):
66
66
lambda df , copy : df .rename (columns = str .lower , copy = copy ),
67
67
lambda df , copy : df .reindex (columns = ["a" , "c" ], copy = copy ),
68
68
lambda df , copy : df .reindex_like (df , copy = copy ),
69
+ lambda df , copy : df .align (df , copy = copy )[0 ],
69
70
lambda df , copy : df .set_axis (["a" , "b" , "c" ], axis = "index" , copy = copy ),
70
71
lambda df , copy : df .rename_axis (index = "test" , copy = copy ),
71
72
lambda df , copy : df .rename_axis (columns = "test" , copy = copy ),
@@ -84,6 +85,7 @@ def test_copy_shallow(using_copy_on_write):
84
85
"rename" ,
85
86
"reindex" ,
86
87
"reindex_like" ,
88
+ "align" ,
87
89
"set_axis" ,
88
90
"rename_axis0" ,
89
91
"rename_axis1" ,
@@ -115,22 +117,96 @@ def test_methods_copy_keyword(
115
117
df = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [0.1 , 0.2 , 0.3 ]}, index = index )
116
118
df2 = method (df , copy = copy )
117
119
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
119
121
120
122
if request .node .callspec .id .startswith ("reindex-" ):
121
123
# TODO copy=False without CoW still returns a copy in this case
122
124
if not using_copy_on_write and not using_array_manager and copy is False :
123
125
share_memory = False
124
- # TODO copy=True with CoW still returns a view
125
- if using_copy_on_write :
126
- share_memory = True
127
126
128
127
if share_memory :
129
128
assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
130
129
else :
131
130
assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
132
131
133
132
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
+
134
210
# -----------------------------------------------------------------------------
135
211
# DataFrame methods returning new DataFrame using shallow copy
136
212
@@ -1119,14 +1195,13 @@ def test_set_flags(using_copy_on_write):
1119
1195
tm .assert_series_equal (ser , expected )
1120
1196
1121
1197
1122
- @pytest .mark .parametrize ("copy_kwargs" , [{"copy" : True }, {}])
1123
1198
@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 ):
1125
1200
df = DataFrame ({"a" : [1 , 2 , 3 , 4 ]}, index = Index ([1 , 2 , 3 , 4 ], name = "a" ))
1126
1201
df_orig = df .copy ()
1127
- df2 = df .rename_axis (** kwargs , ** copy_kwargs )
1202
+ df2 = df .rename_axis (** kwargs )
1128
1203
1129
- if using_copy_on_write and not copy_kwargs :
1204
+ if using_copy_on_write :
1130
1205
assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
1131
1206
else :
1132
1207
assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
0 commit comments