Skip to content

ENH: Raise error in certain unhandled _reduce cases. #8592

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 1 commit into from
Oct 28, 2014
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.15.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Enhancements

- Qualify memory usage in ``DataFrame.info()`` by adding ``+`` if it is a lower bound (:issue:`8578`)

- Raise errors in certain aggregation cases where an argument such as ``numeric_only`` is not handled (:issue:`8592`).


.. _whatsnew_0151.performance:

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,8 @@ def __setitem__(self, key, value):
self._codes[key] = lindexer

#### reduction ops ####
def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
filter_type=None, name=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
""" perform the reduction type operation """
func = getattr(self,name,None)
if func is None:
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4129,7 +4129,7 @@ def any(self, axis=None, bool_only=None, skipna=True, level=None,
if level is not None:
return self._agg_by_level('any', axis=axis, level=level,
skipna=skipna)
return self._reduce(nanops.nanany, axis=axis, skipna=skipna,
return self._reduce(nanops.nanany, 'any', axis=axis, skipna=skipna,
Copy link
Contributor

Choose a reason for hiding this comment

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

where these the only 2 that didn't pass 'name' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's also this line where name wasn't being passed:
https://github.com/pydata/pandas/pull/8592/files#diff-03b380f521c43cf003207b0711bac67fR4007

I think the plan is to remove the above two (any/all) in the separate PR for any/all implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

right, I was meaning any definitions (e.g. make_stat_function etc). no answer looks like no, ok, good.

numeric_only=bool_only, filter_type='bool')

def all(self, axis=None, bool_only=None, skipna=True, level=None,
Expand Down Expand Up @@ -4160,11 +4160,11 @@ def all(self, axis=None, bool_only=None, skipna=True, level=None,
if level is not None:
return self._agg_by_level('all', axis=axis, level=level,
skipna=skipna)
return self._reduce(nanops.nanall, axis=axis, skipna=skipna,
return self._reduce(nanops.nanall, 'all', axis=axis, skipna=skipna,
numeric_only=bool_only, filter_type='bool')

def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
filter_type=None, name=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
axis = self._get_axis_number(axis)
f = lambda x: op(x, axis=axis, skipna=skipna, **kwds)
labels = self._get_agg_axis(axis)
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3934,9 +3934,8 @@ def stat_func(self, axis=None, skipna=None, level=None,
if level is not None:
return self._agg_by_level(name, axis=axis, level=level,
skipna=skipna)
return self._reduce(f, axis=axis,
skipna=skipna, numeric_only=numeric_only,
name=name)
return self._reduce(f, name, axis=axis,
skipna=skipna, numeric_only=numeric_only)
stat_func.__name__ = name
return stat_func

Expand Down Expand Up @@ -4005,7 +4004,7 @@ def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
if level is not None:
return self._agg_by_level(name, axis=axis, level=level,
skipna=skipna, ddof=ddof)
return self._reduce(f, axis=axis,
return self._reduce(f, name, axis=axis,
skipna=skipna, ddof=ddof)
stat_func.__name__ = name
return stat_func
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,12 @@ def _apply_2d(self, func, axis):

return self._construct_return_type(dict(results))

def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
filter_type=None, name=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
if numeric_only:
raise NotImplementedError(
'Panel.{0} does not implement numeric_only.'.format(name))

axis_name = self._get_axis_name(axis)
axis_number = self._get_axis_number(axis_name)
f = lambda x: op(x, axis=axis_number, skipna=skipna, **kwds)
Expand Down
14 changes: 10 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,8 +2056,8 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
return self._constructor(mapped,
index=self.index).__finalize__(self)

def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
filter_type=None, name=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
"""
perform a reduction operation

Expand All @@ -2067,10 +2067,16 @@ def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
"""
delegate = self.values
if isinstance(delegate, np.ndarray):
# Validate that 'axis' is consistent with Series's single axis.
self._get_axis_number(axis)
if numeric_only:
raise NotImplementedError(
'Series.{0} does not implement numeric_only.'.format(name))
return op(delegate, skipna=skipna, **kwds)

return delegate._reduce(op=op, axis=axis, skipna=skipna, numeric_only=numeric_only,
filter_type=filter_type, name=name, **kwds)
return delegate._reduce(op=op, name=name, axis=axis, skipna=skipna,
numeric_only=numeric_only,
filter_type=filter_type, **kwds)

def _maybe_box(self, func, dropna=False):
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def __array_finalize__(self, obj):
self.name = getattr(obj, 'name', None)
self.fill_value = getattr(obj, 'fill_value', None)

def _reduce(self, op, axis=0, skipna=True, numeric_only=None,
filter_type=None, name=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
""" perform a reduction operation """
return op(self.get_values(), skipna=skipna, **kwds)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,11 +1669,11 @@ def test_groupby_multiple_key(self):
lambda x: x.month,
lambda x: x.day], axis=1)

agged = grouped.agg(lambda x: x.sum(1))
agged = grouped.agg(lambda x: x.sum())
self.assertTrue(agged.index.equals(df.columns))
assert_almost_equal(df.T.values, agged.values)

agged = grouped.agg(lambda x: x.sum(1))
agged = grouped.agg(lambda x: x.sum())
assert_almost_equal(df.T.values, agged.values)

def test_groupby_multi_corner(self):
Expand Down Expand Up @@ -1708,7 +1708,7 @@ def test_omit_nuisance(self):
# won't work with axis = 1
grouped = df.groupby({'A': 0, 'C': 0, 'D': 1, 'E': 1}, axis=1)
result = self.assertRaises(TypeError, grouped.agg,
lambda x: x.sum(1, numeric_only=False))
lambda x: x.sum(0, numeric_only=False))

def test_omit_nuisance_python_multiple(self):
grouped = self.three_group.groupby(['A', 'B'])
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=W0612,E1101

from datetime import datetime
from inspect import getargspec
import operator
import nose

Expand Down Expand Up @@ -169,6 +170,11 @@ def wrapper(x):

self.assertRaises(Exception, f, axis=obj.ndim)

# Unimplemented numeric_only parameter.
if 'numeric_only' in getargspec(f).args:
self.assertRaisesRegexp(NotImplementedError, name, f,
numeric_only=True)


class SafeForSparse(object):
_multiprocess_can_split_ = True
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime, timedelta
import operator
import string
from inspect import getargspec
from itertools import product, starmap
from distutils.version import LooseVersion

Expand Down Expand Up @@ -2338,6 +2339,14 @@ def testit():
exp = alternate(s)
self.assertEqual(res, exp)

# Invalid axis.
self.assertRaises(ValueError, f, self.series, axis=1)

# Unimplemented numeric_only parameter.
if 'numeric_only' in getargspec(f).args:
self.assertRaisesRegexp(NotImplementedError, name, f,
self.series, numeric_only=True)

testit()

try:
Expand Down