Skip to content

BUG: Allow non-callable attributes in aggregate function. Fixes GH16405 #16458

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

Merged
merged 3 commits into from
Jun 1, 2017
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Reshaping
- Bug in ``pd.wide_to_long()`` where no error was raised when ``i`` was not a unique identifier (:issue:`16382`)
- Bug in ``Series.isin(..)`` with a list of tuples (:issue:`16394`)
- Bug in construction of a ``DataFrame`` with mixed dtypes including an all-NaT column. (:issue:`16395`)
- Bug in ``DataFrame.agg()`` and ``Series.agg()`` with aggregating on non-callable attributes (:issue:`16405`)


Numeric
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def aggregate(self, func, *args, **kwargs):
def _try_aggregate_string_function(self, arg, *args, **kwargs):
"""
if arg is a string, then try to operate on it:
- try to find a function on ourselves
- try to find a function (or attribute) on ourselves
- try to find a numpy function
- raise
Expand All @@ -387,7 +387,15 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs):

f = getattr(self, arg, None)
if f is not None:
return f(*args, **kwargs)
if callable(f):
return f(*args, **kwargs)

# people may try to aggregate on a non-callable attribute
# but don't let them think they can pass args to it
assert len(args) == 0
assert len([kwarg for kwarg in kwargs
if kwarg not in ['axis', '_level']]) == 0
return f

f = getattr(np, arg, None)
if f is not None:
Expand Down
45 changes: 45 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,48 @@ def test_nuiscance_columns(self):
expected = DataFrame([[6, 6., 'foobarbaz']],
index=['sum'], columns=['A', 'B', 'C'])
assert_frame_equal(result, expected)

def test_non_callable_aggregates(self):

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment of .size here (and how its a property of Series/DataFrame and hence we allow this)

# GH 16405
# 'size' is a property of frame/series
# validate that this is working
df = DataFrame({'A': [None, 2, 3],
'B': [1.0, np.nan, 3.0],
'C': ['foo', None, 'bar']})

# Function aggregate
result = df.agg({'A': 'count'})
expected = pd.Series({'A': 2})

assert_series_equal(result, expected)

# Non-function aggregate
result = df.agg({'A': 'size'})
expected = pd.Series({'A': 3})

assert_series_equal(result, expected)

# Mix function and non-function aggs
result1 = df.agg(['count', 'size'])
result2 = df.agg({'A': ['count', 'size'],
'B': ['count', 'size'],
'C': ['count', 'size']})
expected = pd.DataFrame({'A': {'count': 2, 'size': 3},
'B': {'count': 2, 'size': 3},
'C': {'count': 2, 'size': 3}})

assert_frame_equal(result1, result2, check_like=True)
assert_frame_equal(result2, expected, check_like=True)

# Just functional string arg is same as calling df.arg()
result = df.agg('count')
expected = df.count()

assert_series_equal(result, expected)

# Just a string attribute arg same as calling df.arg
result = df.agg('size')
expected = df.size

assert result == expected
16 changes: 16 additions & 0 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ def test_reduce(self):
name=self.series.name)
assert_series_equal(result, expected)

def test_non_callable_aggregates(self):
# test agg using non-callable series attributes
s = Series([1, 2, None])

# Calling agg w/ just a string arg same as calling s.arg
result = s.agg('size')
expected = s.size
assert result == expected

# test when mixed w/ callable reducers
result = s.agg(['size', 'count', 'mean'])
expected = Series(OrderedDict({'size': 3.0,
'count': 2.0,
'mean': 1.5}))
assert_series_equal(result[expected.index], expected)


class TestSeriesMap(TestData):

Expand Down