@@ -824,6 +824,30 @@ def test_apply_issues(self):
824
824
result = df .groupby ('date' ).apply (lambda x : x ['time' ][x ['value' ].idxmax ()])
825
825
assert_series_equal (result , expected )
826
826
827
+ def test_time_field_bug (self ):
828
+ # Test a fix for the following error related to GH issue 11324
829
+ # When non-key fields in a group-by dataframe contained time-based fields that
830
+ # were not returned by the apply function, an exception would be raised.
831
+
832
+ df = pd .DataFrame ({'a' : 1 ,'b' : [datetime .now () for nn in range (10 )]})
833
+
834
+ def func_with_no_date (batch ):
835
+ return pd .Series ({'c' : 2 })
836
+
837
+ def func_with_date (batch ):
838
+ return pd .Series ({'c' : 2 , 'b' : datetime (2015 , 1 , 1 )})
839
+
840
+ dfg_no_conversion = df .groupby (by = ['a' ]).apply (func_with_no_date )
841
+ dfg_no_conversion_expected = pd .DataFrame ({'c' : 2 }, index = [1 ])
842
+ dfg_no_conversion_expected .index .name = 'a'
843
+
844
+ dfg_conversion = df .groupby (by = ['a' ]).apply (func_with_date )
845
+ dfg_conversion_expected = pd .DataFrame ({'b' : datetime (2015 , 1 , 1 ), 'c' : 2 }, index = [1 ])
846
+ dfg_conversion_expected .index .name = 'a'
847
+
848
+ self .assert_frame_equal (dfg_no_conversion , dfg_no_conversion_expected )
849
+ self .assert_frame_equal (dfg_conversion , dfg_conversion_expected )
850
+
827
851
def test_len (self ):
828
852
df = tm .makeTimeDataFrame ()
829
853
grouped = df .groupby ([lambda x : x .year ,
0 commit comments