From 85bb7521d8a0acb522d210b16484c241ca6498e9 Mon Sep 17 00:00:00 2001 From: Amanda Dsouza Date: Sat, 26 Sep 2020 21:52:37 +0530 Subject: [PATCH 1/2] TST: Added test for groupby apply datetimeindex fix --- pandas/tests/groupby/test_apply.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index db5c4af9c6f53..e317183fea922 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1095,3 +1095,20 @@ def test_apply_by_cols_equals_apply_by_rows_transposed(): tm.assert_frame_equal(by_cols, by_rows.T) tm.assert_frame_equal(by_cols, df) + + +def test_apply_groupby_datetimeindex(): + # GH 26182 + # groupby apply failed on dataframe with DatetimeIndex + + data = [["A", 10], ["B", 20], ["B", 30], ["C", 40], ["C", 50]] + df = pd.DataFrame( + data, columns=["Name", "Value"], index=pd.date_range("2020-09-01", "2020-09-05") + ) + + result = df.groupby("Name").sum() + + expected = pd.DataFrame({"Name": ["A", "B", "C"], "Value": [10, 50, 90]}) + expected.set_index("Name", inplace=True) + + tm.assert_frame_equal(result, expected) From cc34c39bb07a381ecdf06d060f84827134d5d729 Mon Sep 17 00:00:00 2001 From: Amanda Dsouza Date: Sat, 26 Sep 2020 23:08:55 +0530 Subject: [PATCH 2/2] TST: Moved test to similar tests --- pandas/tests/groupby/test_apply.py | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index e317183fea922..176efdb6204da 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -681,6 +681,23 @@ def test_apply_aggregating_timedelta_and_datetime(): tm.assert_frame_equal(result, expected) +def test_apply_groupby_datetimeindex(): + # GH 26182 + # groupby apply failed on dataframe with DatetimeIndex + + data = [["A", 10], ["B", 20], ["B", 30], ["C", 40], ["C", 50]] + df = pd.DataFrame( + data, columns=["Name", "Value"], index=pd.date_range("2020-09-01", "2020-09-05") + ) + + result = df.groupby("Name").sum() + + expected = pd.DataFrame({"Name": ["A", "B", "C"], "Value": [10, 50, 90]}) + expected.set_index("Name", inplace=True) + + tm.assert_frame_equal(result, expected) + + def test_time_field_bug(): # 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 @@ -1095,20 +1112,3 @@ def test_apply_by_cols_equals_apply_by_rows_transposed(): tm.assert_frame_equal(by_cols, by_rows.T) tm.assert_frame_equal(by_cols, df) - - -def test_apply_groupby_datetimeindex(): - # GH 26182 - # groupby apply failed on dataframe with DatetimeIndex - - data = [["A", 10], ["B", 20], ["B", 30], ["C", 40], ["C", 50]] - df = pd.DataFrame( - data, columns=["Name", "Value"], index=pd.date_range("2020-09-01", "2020-09-05") - ) - - result = df.groupby("Name").sum() - - expected = pd.DataFrame({"Name": ["A", "B", "C"], "Value": [10, 50, 90]}) - expected.set_index("Name", inplace=True) - - tm.assert_frame_equal(result, expected)