Skip to content

BUG: agg with axis=1 #19605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4807,11 +4807,21 @@ 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, axis=0, *args, **kwargs)
result, how = self._aggregate(func,
_axis=0,
*args, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though would prefer to pass in axis and have it used down in the stack

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as .apply does generally work with axis=1, it is prob just not passed thru

except TypeError:
pass
elif axis == 1:
try:
result, how = self.T._aggregate(func,
_axis=0,
*args, **kwargs)
result = result.T
except TypeError:
pass
if result is None:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,18 @@ 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],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment.

'C': [7, 8, 9, 10, 11]},
index=[1, 2, 3, 4, 5])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than just adding a new test. ideally would like to systematically test all routines.
you can create a fixture

@pytest.fixture(params=[0, 1])
def axis(request):
    return request.param

assert_series_equal(df.agg('min', axis=1),
Series({1: 4,
2: 5,
3: 6,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add a test with mixed dtypes (note that they will generally be excluded on the agg so this works ok)

4: 7,
5: 8}))

assert_series_equal(df.agg('min', axis=1),
df.T.agg('min').T)