Skip to content

Commit f9de1ba

Browse files
committed
ENH: add weights kw to numeric aggregation functions
closes pandas-dev#10030
1 parent 74e20a0 commit f9de1ba

File tree

11 files changed

+532
-331
lines changed

11 files changed

+532
-331
lines changed

pandas/core/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -910,12 +910,18 @@ def hasnans(self):
910910
return isnull(self).any()
911911

912912
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
913-
filter_type=None, **kwds):
913+
weights=None, filter_type=None, **kwds):
914914
""" perform the reduction type operation if we can """
915915
func = getattr(self, name, None)
916916
if func is None:
917917
raise TypeError("{klass} cannot perform the operation {op}".format(
918918
klass=self.__class__.__name__, op=name))
919+
920+
if weights is not None:
921+
from pandas.tools import weightby
922+
_, weights = weightby.weightby(self, weights=weights, axis=axis)
923+
kwds['weights'] = weights
924+
919925
return func(**kwds)
920926

921927
def value_counts(self, normalize=False, sort=True, ascending=False,

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ def _reverse_indexer(self):
17471747

17481748
# reduction ops #
17491749
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
1750-
filter_type=None, **kwds):
1750+
weights=None, filter_type=None, **kwds):
17511751
""" perform the reduction type operation """
17521752
func = getattr(self, name, None)
17531753
if func is None:

pandas/core/frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -4894,10 +4894,15 @@ def _count_level(self, level, axis=0, numeric_only=False):
48944894
else:
48954895
return result
48964896

4897-
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
4898-
filter_type=None, **kwds):
4897+
def _reduce(self, op, name, axis=0, skipna=True, weights=None,
4898+
numeric_only=None, filter_type=None, **kwds):
48994899
axis = self._get_axis_number(axis)
49004900

4901+
if weights is not None:
4902+
from pandas.tools import weightby
4903+
self, weights = weightby.weightby(self, weights=weights, axis=axis)
4904+
kwds['weights'] = weights
4905+
49014906
def f(x):
49024907
return op(x, axis=axis, skipna=skipna, **kwds)
49034908

0 commit comments

Comments
 (0)