Skip to content

WIP/ENH: add weights kw to numeric aggregation functions #15039

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
wants to merge 3 commits into from
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
9 changes: 6 additions & 3 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,16 @@ def validate_expanding_func(name, args, kwargs):
raise UnsupportedFunctionCall(msg)


def validate_groupby_func(name, args, kwargs):
def validate_groupby_func(name, args, kwargs, allowed_kwargs=None):
"""
'args' and 'kwargs' should be empty because all of
'args' should be empty because all of
their necessary parameters are explicitly listed in
the function signature
"""
if len(args) + len(kwargs) > 0:
if allowed_kwargs:
kwargs = set(kwargs) - set(allowed_kwargs)

if len(args) or len(kwargs):
raise UnsupportedFunctionCall((
"numpy operations are not valid "
"with groupby. Use .groupby(...)."
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,18 @@ def hasnans(self):
return isnull(self).any()

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
weights=None, filter_type=None, **kwds):
""" perform the reduction type operation if we can """
func = getattr(self, name, None)
if func is None:
raise TypeError("{klass} cannot perform the operation {op}".format(
klass=self.__class__.__name__, op=name))

if weights is not None:
from pandas.tools import weightby
_, weights = weightby.weightby(self, weights=weights, axis=axis)
kwds['weights'] = weights

return func(**kwds)

def value_counts(self, normalize=False, sort=True, ascending=False,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ def _reverse_indexer(self):

# reduction ops #
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
weights=None, filter_type=None, **kwds):
""" perform the reduction type operation """
func = getattr(self, name, None)
if func is None:
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4894,10 +4894,15 @@ def _count_level(self, level, axis=0, numeric_only=False):
else:
return result

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
def _reduce(self, op, name, axis=0, skipna=True, weights=None,
numeric_only=None, filter_type=None, **kwds):
axis = self._get_axis_number(axis)

if weights is not None:
from pandas.tools import weightby
self, weights = weightby.weightby(self, weights=weights, axis=axis)
kwds['weights'] = weights

def f(x):
return op(x, axis=axis, skipna=skipna, **kwds)

Expand Down
Loading