diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 0561b3a1d8592..6756d25ad1707 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -233,6 +233,36 @@ def test_apply_issues(self): lambda x: x['time'][x['value'].idxmax()]) assert_series_equal(result, expected) + def test_apply_trivial(self): + # GH 20066 + # trivial apply: ignore input and return a constant dataframe. + df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'], + 'data': [1.0, 2.0, 3.0, 4.0, 5.0]}, + columns=['key', 'data']) + expected = pd.concat([df.iloc[1:], df.iloc[1:]], + axis=1, keys=['float64', 'object']) + result = df.groupby([str(x) for x in df.dtypes], + axis=1).apply(lambda x: df.iloc[1:]) + + assert_frame_equal(result, expected) + + @pytest.mark.xfail(reason=("GH 20066; function passed into apply " + "returns a DataFrame with the same index " + "as the one to create GroupBy object.")) + def test_apply_trivial_fail(self): + # GH 20066 + # trivial apply fails if the constant dataframe has the same index + # with the one used to create GroupBy object. + df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'], + 'data': [1.0, 2.0, 3.0, 4.0, 5.0]}, + columns=['key', 'data']) + expected = pd.concat([df, df], + axis=1, keys=['float64', 'object']) + result = df.groupby([str(x) for x in df.dtypes], + axis=1).apply(lambda x: df) + + assert_frame_equal(result, expected) + def test_time_field_bug(self): # Test a fix for the following error related to GH issue 11324 When # non-key fields in a group-by dataframe contained time-based fields