From e5bbf227aee0cc7ee4af7d3ffdf624ed226b35f5 Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Thu, 8 Feb 2018 16:26:46 -0500 Subject: [PATCH 1/7] A very small change to pass the axis arg correctly down the stack of aggregate functions. Provides the user with a correct Exception for this issue. --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 28923f0fbf240..b9ea3a1fe3d58 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4809,9 +4809,9 @@ def aggregate(self, func, axis=0, *args, **kwargs): # TODO: flipped axis result = None - if axis == 0: + if axis == 0 or axis == 1: try: - result, how = self._aggregate(func, axis=0, *args, **kwargs) + result, how = self._aggregate(func, _axis=axis, *args, **kwargs) except TypeError: pass if result is None: From 66b8b9d0eb3c7ee9dc63f856eb210f4693b7c09a Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Thu, 8 Feb 2018 16:50:07 -0500 Subject: [PATCH 2/7] Formating change to pass flake8 --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b9ea3a1fe3d58..03c55673e7bcf 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4811,7 +4811,9 @@ def aggregate(self, func, axis=0, *args, **kwargs): result = None if axis == 0 or axis == 1: try: - result, how = self._aggregate(func, _axis=axis, *args, **kwargs) + result, how = self._aggregate(func, + _axis=axis, + *args, **kwargs) except TypeError: pass if result is None: From 4b80516728704a7c2d87f2251a148833a548c147 Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Thu, 8 Feb 2018 17:06:54 -0500 Subject: [PATCH 3/7] Implement a transpose wrapper around column wise aggregation. --- pandas/core/frame.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 03c55673e7bcf..346e1b6e798c5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4809,13 +4809,18 @@ def aggregate(self, func, axis=0, *args, **kwargs): # TODO: flipped axis result = None - if axis == 0 or axis == 1: + if axis == 0: try: result, how = self._aggregate(func, - _axis=axis, + _axis=0, *args, **kwargs) except TypeError: pass + elif axis == 1: + try: + result, how = self.T._aggregate(func, + _axis=0, + *args, **kwargs).T if result is None: return self.apply(func, axis=axis, args=args, **kwargs) return result From f5698d5abaee1eea79cd0a58a3b84bd95d7d735e Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Thu, 8 Feb 2018 17:09:20 -0500 Subject: [PATCH 4/7] Add exception block --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 346e1b6e798c5..ae41b15e9b17b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4807,8 +4807,8 @@ def _gotitem(self, key, ndim, subset=None): def aggregate(self, func, axis=0, *args, **kwargs): axis = self._get_axis_number(axis) - # TODO: flipped axis result = None + if axis == 0: try: result, how = self._aggregate(func, @@ -4821,6 +4821,8 @@ def aggregate(self, func, axis=0, *args, **kwargs): result, how = self.T._aggregate(func, _axis=0, *args, **kwargs).T + except TypeError: + pass if result is None: return self.apply(func, axis=axis, args=args, **kwargs) return result From 1c076a289b57322b2a170e495a77b59e638c3b22 Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Thu, 8 Feb 2018 17:14:40 -0500 Subject: [PATCH 5/7] Correctly transpose just the result, not the returned tuple --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ae41b15e9b17b..e734882486b14 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4820,7 +4820,8 @@ def aggregate(self, func, axis=0, *args, **kwargs): try: result, how = self.T._aggregate(func, _axis=0, - *args, **kwargs).T + *args, **kwargs) + result = result.T except TypeError: pass if result is None: From 5a261fce832a10d2929639db33b88a0876632f18 Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Fri, 9 Feb 2018 14:56:36 -0500 Subject: [PATCH 6/7] Add test to cover addition to frame.aggregate for axis=1 aggregation --- pandas/tests/frame/test_apply.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index d1ad9f71e6350..6317a827e8d97 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -1012,3 +1012,14 @@ def test_non_callable_aggregates(self): expected = df.size assert result == expected + + def test_frame_row_and_column_aggregates(self): + df = DataFrame({'B':[4,5,6,7,8], + 'C':[7,8,9,10,11],}, + index=[1,2,3,4,5]) + + assert_series_equal(df.agg('min',axis=1), + Series({1:4,2:5,3:6,4:7,5:8})) + + assert_series_equal(df.agg('min',axis=1), + df.T.agg('min').T) From 054a13ac9b07995c531cbf7e48ba4d8402421cff Mon Sep 17 00:00:00 2001 From: Spencer Ogden Date: Fri, 9 Feb 2018 15:52:32 -0500 Subject: [PATCH 7/7] Fix PEP8 formatting in test --- pandas/tests/frame/test_apply.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 6317a827e8d97..cc07fae535723 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -1014,12 +1014,16 @@ def test_non_callable_aggregates(self): assert result == expected def test_frame_row_and_column_aggregates(self): - df = DataFrame({'B':[4,5,6,7,8], - 'C':[7,8,9,10,11],}, - index=[1,2,3,4,5]) - - assert_series_equal(df.agg('min',axis=1), - Series({1:4,2:5,3:6,4:7,5:8})) - - assert_series_equal(df.agg('min',axis=1), + df = DataFrame({'B': [4, 5, 6, 7, 8], + 'C': [7, 8, 9, 10, 11]}, + index=[1, 2, 3, 4, 5]) + + assert_series_equal(df.agg('min', axis=1), + Series({1: 4, + 2: 5, + 3: 6, + 4: 7, + 5: 8})) + + assert_series_equal(df.agg('min', axis=1), df.T.agg('min').T)