-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Added a min_count keyword to stat funcs #18876
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7322,7 +7322,8 @@ def _add_numeric_operations(cls): | |
@Substitution(outname='mad', | ||
desc="Return the mean absolute deviation of the values " | ||
"for the requested axis", | ||
name1=name, name2=name2, axis_descr=axis_descr) | ||
name1=name, name2=name2, axis_descr=axis_descr, | ||
min_count='', examples='') | ||
@Appender(_num_doc) | ||
def mad(self, axis=None, skipna=None, level=None): | ||
if skipna is None: | ||
|
@@ -7363,7 +7364,8 @@ def mad(self, axis=None, skipna=None, level=None): | |
@Substitution(outname='compounded', | ||
desc="Return the compound percentage of the values for " | ||
"the requested axis", name1=name, name2=name2, | ||
axis_descr=axis_descr) | ||
axis_descr=axis_descr, | ||
min_count='', examples='') | ||
@Appender(_num_doc) | ||
def compound(self, axis=None, skipna=None, level=None): | ||
if skipna is None: | ||
|
@@ -7387,10 +7389,10 @@ def compound(self, axis=None, skipna=None, level=None): | |
lambda y, axis: np.maximum.accumulate(y, axis), "max", | ||
-np.inf, np.nan) | ||
|
||
cls.sum = _make_stat_function( | ||
cls.sum = _make_min_count_stat_function( | ||
cls, 'sum', name, name2, axis_descr, | ||
'Return the sum of the values for the requested axis', | ||
nanops.nansum) | ||
nanops.nansum, _sum_examples) | ||
cls.mean = _make_stat_function( | ||
cls, 'mean', name, name2, axis_descr, | ||
'Return the mean of the values for the requested axis', | ||
|
@@ -7406,10 +7408,10 @@ def compound(self, axis=None, skipna=None, level=None): | |
"by N-1\n", | ||
nanops.nankurt) | ||
cls.kurtosis = cls.kurt | ||
cls.prod = _make_stat_function( | ||
cls.prod = _make_min_count_stat_function( | ||
cls, 'prod', name, name2, axis_descr, | ||
'Return the product of the values for the requested axis', | ||
nanops.nanprod) | ||
nanops.nanprod, _prod_examples) | ||
cls.product = cls.prod | ||
cls.median = _make_stat_function( | ||
cls, 'median', name, name2, axis_descr, | ||
|
@@ -7540,10 +7542,13 @@ def _doc_parms(cls): | |
numeric_only : boolean, default None | ||
Include only float, int, boolean columns. If None, will attempt to use | ||
everything, then use only numeric data. Not implemented for Series. | ||
%(min_count)s\ | ||
|
||
Returns | ||
------- | ||
%(outname)s : %(name1)s or %(name2)s (if level specified)\n""" | ||
%(outname)s : %(name1)s or %(name2)s (if level specified) | ||
|
||
%(examples)s""" | ||
|
||
_num_ddof_doc = """ | ||
|
||
|
@@ -7611,9 +7616,92 @@ def _doc_parms(cls): | |
""" | ||
|
||
|
||
_sum_examples = """\ | ||
Examples | ||
-------- | ||
By default, the sum of an empty series is ``NaN``. | ||
|
||
>>> pd.Series([]).sum() # min_count=1 is the default | ||
nan | ||
|
||
This can be controlled with the ``min_count`` parameter. For example, if | ||
you'd like the sum of an empty series to be 0, pass ``min_count=0``. | ||
|
||
>>> pd.Series([]).sum(min_count=0) | ||
0.0 | ||
|
||
Thanks to the ``skipna`` parameter, ``min_count`` handles all-NA and | ||
empty series identically. | ||
|
||
>>> pd.Series([np.nan]).sum() | ||
nan | ||
|
||
>>> pd.Series([np.nan]).sum(min_count=0) | ||
0.0 | ||
""" | ||
|
||
_prod_examples = """\ | ||
Examples | ||
-------- | ||
By default, the product of an empty series is ``NaN`` | ||
|
||
>>> pd.Series([]).prod() | ||
nan | ||
|
||
This can be controlled with the ``min_count`` parameter | ||
|
||
>>> pd.Series([]).prod(min_count=0) | ||
1.0 | ||
|
||
Thanks to the ``skipna`` parameter, ``min_count`` handles all-NA and | ||
empty series identically. | ||
|
||
>>> pd.Series([np.nan]).prod() | ||
nan | ||
|
||
>>> pd.Series([np.nan]).sum(min_count=0) | ||
1.0 | ||
""" | ||
|
||
|
||
_min_count_stub = """\ | ||
min_count : int, default 1 | ||
The required number of valid values to perform the operation. If fewer than | ||
``min_count`` non-NA values are present the result will be NA. | ||
|
||
.. versionadded :: 0.21.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably it will become 0.22 ? (but can change later) |
||
|
||
Added with the default being 1. This means the sum or product | ||
of an all-NA or empty series is ``NaN``. | ||
""" | ||
|
||
|
||
def _make_min_count_stat_function(cls, name, name1, name2, axis_descr, desc, | ||
f, examples): | ||
@Substitution(outname=name, desc=desc, name1=name1, name2=name2, | ||
axis_descr=axis_descr, min_count=_min_count_stub, | ||
examples=examples) | ||
@Appender(_num_doc) | ||
def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None, | ||
min_count=1, | ||
**kwargs): | ||
nv.validate_stat_func(tuple(), kwargs, fname=name) | ||
if skipna is None: | ||
skipna = True | ||
if axis is None: | ||
axis = self._stat_axis_number | ||
if level is not None: | ||
return self._agg_by_level(name, axis=axis, level=level, | ||
skipna=skipna, min_count=min_count) | ||
return self._reduce(f, name, axis=axis, skipna=skipna, | ||
numeric_only=numeric_only, min_count=min_count) | ||
|
||
return set_function_name(stat_func, name, cls) | ||
|
||
|
||
def _make_stat_function(cls, name, name1, name2, axis_descr, desc, f): | ||
@Substitution(outname=name, desc=desc, name1=name1, name2=name2, | ||
axis_descr=axis_descr) | ||
axis_descr=axis_descr, min_count='', examples='') | ||
@Appender(_num_doc) | ||
def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None, | ||
**kwargs): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this work for
min_count==0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah,
sumx
starts out aszeros
, so we just have to avoid setting it to NaN. Same forprod
, but with ones.