@@ -190,6 +190,46 @@ def f_constant_df(group):
190190 assert names == group_names
191191
192192
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+
227+ df = pd .DataFrame ({"g" : [1 , 2 , 2 , 2 ], "a" : [1 , 2 , 3 , 4 ], "b" : [5 , 6 , 7 , 8 ]})
228+
229+ result = df .groupby ("g" ).apply (func )
230+ tm .assert_frame_equal (result , df )
231+
232+
193233def test_apply_with_mixed_dtype ():
194234 # GH3480, apply with mixed dtype on axis=1 breaks in 0.11
195235 df = DataFrame (
@@ -921,39 +961,3 @@ def fn(x):
921961 name = "col2" ,
922962 )
923963 tm .assert_series_equal (result , expected )
924-
925-
926- def test_apply_fast_slow_identical ():
927- # GH 31613
928-
929- df = DataFrame ({"A" : [0 , 0 , 1 ], "b" : range (3 )})
930-
931- # For simple index structures we check for fast/slow apply using
932- # an identity check on in/output
933- def slow (group ):
934- return group
935-
936- def fast (group ):
937- return group .copy ()
938-
939- fast_df = df .groupby ("A" ).apply (fast )
940- slow_df = df .groupby ("A" ).apply (slow )
941-
942- tm .assert_frame_equal (fast_df , slow_df )
943-
944-
945- def test_gh14927 ():
946- # GH 14927
947- df = pd .DataFrame ({"g" : [1 , 2 , 2 , 2 ], "a" : [1 , 2 , 3 , 4 ], "b" : [5 , 6 , 7 , 8 ]})
948-
949- df1 = df .groupby ("g" ).apply (lambda x : x )
950-
951- df2 = df .groupby ("g" ).apply (lambda x : x [:])
952-
953- df3 = df .groupby ("g" ).apply (lambda x : x .copy (deep = False ))
954-
955- df4 = df .groupby ("g" ).apply (lambda x : x .copy (deep = True ))
956-
957- tm .assert_frame_equal (df1 , df2 )
958- tm .assert_frame_equal (df2 , df3 )
959- tm .assert_frame_equal (df3 , df4 )
0 commit comments