From 7e5eb1b520568ac7ce588bea51e7e3c5d7c7a887 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sun, 30 May 2021 01:40:41 +0800 Subject: [PATCH 01/11] resolve different types if the DataFrame is empty --- pandas/core/frame.py | 3 +++ pandas/tests/frame/test_aggregate.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 pandas/tests/frame/test_aggregate.py diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6e71cb49596c8..5ccc5f20875b2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8534,6 +8534,9 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs): result_in_dict = relabel_result(result, func, columns, order) result = DataFrame(result_in_dict, index=columns) + if isinstance(result, DataFrame): + result = result.squeeze() + return result agg = aggregate diff --git a/pandas/tests/frame/test_aggregate.py b/pandas/tests/frame/test_aggregate.py new file mode 100644 index 0000000000000..8f7c12fc53f95 --- /dev/null +++ b/pandas/tests/frame/test_aggregate.py @@ -0,0 +1,23 @@ +import numpy as np + +import pandas as pd + +from pandas import ( + DataFrame, + Series, + date_range, + timedelta_range, +) + +import pandas._testing as tm + + +def _check_mixed_int(df, dtype=None): + # GH#41672 + result = DataFrame([], columns=['lang', 'name']) + result = result.agg({'name': lambda y: y.values}) + assert type(result) == Series + + result = DataFrame([['a', 'boof']], columns=['lang', 'name']) + result = result.agg({'name': lambda y: y.values}) + assert type(result) == Series From ff4e0ad2a41f0461bd525355edd340fe3666bd6e Mon Sep 17 00:00:00 2001 From: weikhor Date: Sun, 30 May 2021 09:09:28 +0800 Subject: [PATCH 02/11] resolve line spacing --- pandas/tests/frame/test_aggregate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/frame/test_aggregate.py b/pandas/tests/frame/test_aggregate.py index 8f7c12fc53f95..16d769afb93f1 100644 --- a/pandas/tests/frame/test_aggregate.py +++ b/pandas/tests/frame/test_aggregate.py @@ -1,14 +1,12 @@ import numpy as np import pandas as pd - from pandas import ( DataFrame, Series, date_range, timedelta_range, ) - import pandas._testing as tm From e4135bc6a84616569f82089a86132d503b10b5ec Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Feb 2022 22:46:19 +0800 Subject: [PATCH 03/11] add --- pandas/tests/groupby/test_categorical.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index a33e4efbe3b6d..cb52dd58b6774 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1794,3 +1794,24 @@ def test_groupby_categorical_observed_nunique(): name="c", ) tm.assert_series_equal(result, expected) + +def test_groupby_categorical_aggregate_functions(): + #37275 + dtype = pd.CategoricalDtype(categories=["small", "big"], ordered=True) + df = pd.DataFrame([ + [1, "small"], + [1, "big"], + [2, "small"] + ], columns=["grp", "description"]).astype({"description": dtype}) + + + result = df.groupby("grp")["description"].max() + expected = Series( + ["big", "small"], + index=pd.Index([1, 2], name="grp"), + name="description", + dtype=pd.CategoricalDtype(categories=["small","big"], ordered=True) + ) + + tm.assert_series_equal(result, expected) + From a46572d1f6afd5b1542c8f607acd1b2516d88453 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Feb 2022 22:47:08 +0800 Subject: [PATCH 04/11] add testcase for groupby categorical aggregate functions --- pandas/tests/groupby/test_categorical.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index cb52dd58b6774..ac5e44b508e53 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1804,7 +1804,6 @@ def test_groupby_categorical_aggregate_functions(): [2, "small"] ], columns=["grp", "description"]).astype({"description": dtype}) - result = df.groupby("grp")["description"].max() expected = Series( ["big", "small"], From f8a9b6bd3a75a19fad43113f106b3087ad812f15 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Feb 2022 23:01:55 +0800 Subject: [PATCH 05/11] autopep8 --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5c193dfbacb8a..8274b5850bb4c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8738,7 +8738,7 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs): if isinstance(result, DataFrame): result = result.squeeze() - + return result agg = aggregate From 0b5f5edb4db416cf70b7f46e1ab892fabc575ffb Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Feb 2022 23:05:11 +0800 Subject: [PATCH 06/11] autopep8 --- pandas/tests/groupby/test_categorical.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index ac5e44b508e53..908f7fa490e91 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1795,8 +1795,9 @@ def test_groupby_categorical_observed_nunique(): ) tm.assert_series_equal(result, expected) + def test_groupby_categorical_aggregate_functions(): - #37275 + # GH#37275 dtype = pd.CategoricalDtype(categories=["small", "big"], ordered=True) df = pd.DataFrame([ [1, "small"], @@ -1806,11 +1807,10 @@ def test_groupby_categorical_aggregate_functions(): result = df.groupby("grp")["description"].max() expected = Series( - ["big", "small"], - index=pd.Index([1, 2], name="grp"), - name="description", - dtype=pd.CategoricalDtype(categories=["small","big"], ordered=True) - ) + ["big", "small"], + index=pd.Index([1, 2], name="grp"), + name="description", + dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True) + ) tm.assert_series_equal(result, expected) - From 67af684ecfe95ed3526b3fe398db3f89abce87fa Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Tue, 1 Feb 2022 23:52:27 +0800 Subject: [PATCH 07/11] Update test_categorical.py --- pandas/tests/groupby/test_categorical.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 908f7fa490e91..933efa7d49020 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1799,18 +1799,16 @@ def test_groupby_categorical_observed_nunique(): def test_groupby_categorical_aggregate_functions(): # GH#37275 dtype = pd.CategoricalDtype(categories=["small", "big"], ordered=True) - df = pd.DataFrame([ - [1, "small"], - [1, "big"], - [2, "small"] - ], columns=["grp", "description"]).astype({"description": dtype}) + df = pd.DataFrame( + [[1, "small"], [1, "big"], [2, "small"]], columns=["grp", "description"] + ).astype({"description": dtype}) result = df.groupby("grp")["description"].max() expected = Series( ["big", "small"], index=pd.Index([1, 2], name="grp"), name="description", - dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True) + dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True), ) tm.assert_series_equal(result, expected) From 42fa2d540466bd8afc40572265daf98f84b8aaa6 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 2 Feb 2022 12:23:23 +0800 Subject: [PATCH 08/11] pre commit --- pandas/core/frame.py | 3 --- pandas/tests/frame/test_aggregate.py | 21 --------------------- 2 files changed, 24 deletions(-) delete mode 100644 pandas/tests/frame/test_aggregate.py diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8274b5850bb4c..5674c118f63d6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -8736,9 +8736,6 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs): result_in_dict = relabel_result(result, func, columns, order) result = DataFrame(result_in_dict, index=columns) - if isinstance(result, DataFrame): - result = result.squeeze() - return result agg = aggregate diff --git a/pandas/tests/frame/test_aggregate.py b/pandas/tests/frame/test_aggregate.py deleted file mode 100644 index 16d769afb93f1..0000000000000 --- a/pandas/tests/frame/test_aggregate.py +++ /dev/null @@ -1,21 +0,0 @@ -import numpy as np - -import pandas as pd -from pandas import ( - DataFrame, - Series, - date_range, - timedelta_range, -) -import pandas._testing as tm - - -def _check_mixed_int(df, dtype=None): - # GH#41672 - result = DataFrame([], columns=['lang', 'name']) - result = result.agg({'name': lambda y: y.values}) - assert type(result) == Series - - result = DataFrame([['a', 'boof']], columns=['lang', 'name']) - result = result.agg({'name': lambda y: y.values}) - assert type(result) == Series From f44b3a67b71c83cd748e361a2d6ff87ee534d51e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 2 Feb 2022 12:35:15 +0800 Subject: [PATCH 09/11] pre commit --- pandas/tests/groupby/test_categorical.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 933efa7d49020..0e72899818139 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1799,14 +1799,14 @@ def test_groupby_categorical_observed_nunique(): def test_groupby_categorical_aggregate_functions(): # GH#37275 dtype = pd.CategoricalDtype(categories=["small", "big"], ordered=True) - df = pd.DataFrame( + df = DataFrame( [[1, "small"], [1, "big"], [2, "small"]], columns=["grp", "description"] ).astype({"description": dtype}) result = df.groupby("grp")["description"].max() expected = Series( ["big", "small"], - index=pd.Index([1, 2], name="grp"), + index=Index([1, 2], name="grp"), name="description", dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True), ) From 1bbf6ffa19daf9d4135b94fb9bda250fc8e6348f Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Wed, 2 Feb 2022 12:49:48 +0800 Subject: [PATCH 10/11] Update test_categorical.py --- pandas/tests/groupby/test_categorical.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 0e72899818139..031e64cc425db 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1811,4 +1811,5 @@ def test_groupby_categorical_aggregate_functions(): dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True), ) + tm.assert_series_equal(result, expected) From 242421d51c0c614ccc70b3db70f95d3d6d85c5be Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Wed, 2 Feb 2022 12:50:08 +0800 Subject: [PATCH 11/11] Update test_categorical.py --- pandas/tests/groupby/test_categorical.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 031e64cc425db..0e72899818139 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1811,5 +1811,4 @@ def test_groupby_categorical_aggregate_functions(): dtype=pd.CategoricalDtype(categories=["small", "big"], ordered=True), ) - tm.assert_series_equal(result, expected)