From 5a5aabd8be315dd131ffbe0e6fbcc312efc11ee3 Mon Sep 17 00:00:00 2001 From: prakhar987 Date: Tue, 26 Nov 2019 22:23:37 +0530 Subject: [PATCH 1/2] TST added test for groupby agg on mulitlevel column (#29772) --- pandas/tests/groupby/aggregate/test_aggregate.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index ea986058616d7..ac8d56034af4c 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -92,6 +92,18 @@ def test_groupby_aggregation_mixed_dtype(): tm.assert_frame_equal(result, expected) +def test_groupby_aggregation_multi_level_column(): + lst = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, np.nan, 6], [4, 5, 6, 7]] + df = pd.DataFrame(np.array(lst), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]]) + + result = df.groupby(level=1, axis=1).sum() + + lst = [[4, 6], [6, 8], [3, 10], [10, 12]] + expected = pd.DataFrame(np.array(lst), dtype="float64") + + tm.assert_frame_equal(result, expected) + + def test_agg_apply_corner(ts, tsframe): # nothing to group, all NA grouped = ts.groupby(ts * np.nan) From 1229c49a4855e43dec7f3db34862a138551124e9 Mon Sep 17 00:00:00 2001 From: prakhar987 Date: Wed, 27 Nov 2019 20:25:59 +0530 Subject: [PATCH 2/2] Modified test to fail on pandas 0.25.x --- .../tests/groupby/aggregate/test_aggregate.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index ac8d56034af4c..e4de2147586f5 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -93,13 +93,20 @@ def test_groupby_aggregation_mixed_dtype(): def test_groupby_aggregation_multi_level_column(): - lst = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, np.nan, 6], [4, 5, 6, 7]] - df = pd.DataFrame(np.array(lst), columns=[["a", "a", "b", "b"], [0, 1, 0, 1]]) + # GH 29772 + lst = [ + [True, True, True, False], + [True, False, np.nan, False], + [True, True, np.nan, False], + [True, True, np.nan, False], + ] + df = pd.DataFrame( + data=lst, + columns=pd.MultiIndex.from_tuples([("A", 0), ("A", 1), ("B", 0), ("B", 1)]), + ) result = df.groupby(level=1, axis=1).sum() - - lst = [[4, 6], [6, 8], [3, 10], [10, 12]] - expected = pd.DataFrame(np.array(lst), dtype="float64") + expected = pd.DataFrame({0: [2.0, 1, 1, 1], 1: [1, 0, 1, 1]}) tm.assert_frame_equal(result, expected)