@@ -190,6 +190,48 @@ def f_constant_df(group):
190
190
assert names == group_names
191
191
192
192
193
+ def test_apply_fast_slow_identical ():
194
+ # GH 31613
195
+
196
+ df = DataFrame ({"A" : [0 , 0 , 1 ], "b" : range (3 )})
197
+
198
+ # For simple index structures we check for fast/slow apply using
199
+ # an identity check on in/output
200
+ def slow (group ):
201
+ return group
202
+
203
+ def fast (group ):
204
+ return group .copy ()
205
+
206
+ fast_df = df .groupby ("A" ).apply (fast )
207
+ slow_df = df .groupby ("A" ).apply (slow )
208
+
209
+ tm .assert_frame_equal (fast_df , slow_df )
210
+
211
+
212
+ @pytest .mark .parametrize (
213
+ "func" ,
214
+ [
215
+ lambda x : x ,
216
+ lambda x : x [:],
217
+ lambda x : x .copy (deep = False ),
218
+ lambda x : x .copy (deep = True ),
219
+ ],
220
+ )
221
+ def test_groupby_apply_identity_maybecopy_index_identical (func ):
222
+ # GH 14927
223
+ # Whether the function returns a copy of the input data or not should not
224
+ # have an impact on the index structure of the result since this is not
225
+ # transparent to the user
226
+ def func (x ):
227
+ return x .copy (deep = True )
228
+
229
+ df = pd .DataFrame ({"g" : [1 , 2 , 2 , 2 ], "a" : [1 , 2 , 3 , 4 ], "b" : [5 , 6 , 7 , 8 ]})
230
+
231
+ result = df .groupby ("g" ).apply (func )
232
+ tm .assert_frame_equal (result , df )
233
+
234
+
193
235
def test_apply_with_mixed_dtype ():
194
236
# GH3480, apply with mixed dtype on axis=1 breaks in 0.11
195
237
df = DataFrame (
@@ -901,39 +943,3 @@ def fn(x):
901
943
name = "col2" ,
902
944
)
903
945
tm .assert_series_equal (result , expected )
904
-
905
-
906
- def test_apply_fast_slow_identical ():
907
- # GH 31613
908
-
909
- df = DataFrame ({"A" : [0 , 0 , 1 ], "b" : range (3 )})
910
-
911
- # For simple index structures we check for fast/slow apply using
912
- # an identity check on in/output
913
- def slow (group ):
914
- return group
915
-
916
- def fast (group ):
917
- return group .copy ()
918
-
919
- fast_df = df .groupby ("A" ).apply (fast )
920
- slow_df = df .groupby ("A" ).apply (slow )
921
-
922
- tm .assert_frame_equal (fast_df , slow_df )
923
-
924
-
925
- def test_gh14927 ():
926
- # GH 14927
927
- df = pd .DataFrame ({"g" : [1 , 2 , 2 , 2 ], "a" : [1 , 2 , 3 , 4 ], "b" : [5 , 6 , 7 , 8 ]})
928
-
929
- df1 = df .groupby ("g" ).apply (lambda x : x )
930
-
931
- df2 = df .groupby ("g" ).apply (lambda x : x [:])
932
-
933
- df3 = df .groupby ("g" ).apply (lambda x : x .copy (deep = False ))
934
-
935
- df4 = df .groupby ("g" ).apply (lambda x : x .copy (deep = True ))
936
-
937
- tm .assert_frame_equal (df1 , df2 )
938
- tm .assert_frame_equal (df2 , df3 )
939
- tm .assert_frame_equal (df3 , df4 )
0 commit comments