-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame[td64].sum(skipna=False) #37148
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
854da9e
BUG: DataFrame[td64].sum(skipna=False)
jbrockmendel f1c536c
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel f2d1e7f
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 58182aa
annotate, privatize
jbrockmendel 1c5bb0f
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 482ac89
annotate
jbrockmendel 1004ca6
Merge branch 'master' of https://github.com/pandas-dev/pandas into bu…
jbrockmendel 3987063
calculate mask in mask_datetimelike_result
jbrockmendel 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 |
---|---|---|
|
@@ -327,7 +327,10 @@ def _na_ok_dtype(dtype: DtypeObj) -> bool: | |
|
||
def _wrap_results(result, dtype: DtypeObj, fill_value=None): | ||
""" wrap our results if needed """ | ||
if is_datetime64_any_dtype(dtype): | ||
if result is NaT: | ||
pass | ||
|
||
elif is_datetime64_any_dtype(dtype): | ||
if fill_value is None: | ||
# GH#24293 | ||
fill_value = iNaT | ||
|
@@ -498,18 +501,45 @@ def nansum( | |
>>> nanops.nansum(s) | ||
3.0 | ||
""" | ||
orig_values = values | ||
|
||
values, mask, dtype, dtype_max, _ = _get_values( | ||
values, skipna, fill_value=0, mask=mask | ||
) | ||
dtype_sum = dtype_max | ||
datetimelike = False | ||
if is_float_dtype(dtype): | ||
dtype_sum = dtype | ||
elif is_timedelta64_dtype(dtype): | ||
datetimelike = True | ||
dtype_sum = np.float64 | ||
|
||
the_sum = values.sum(axis, dtype=dtype_sum) | ||
the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count) | ||
|
||
return _wrap_results(the_sum, dtype) | ||
the_sum = _wrap_results(the_sum, dtype) | ||
if datetimelike and not skipna: | ||
the_sum = _mask_datetimelike_result(the_sum, axis, mask, orig_values) | ||
return the_sum | ||
|
||
|
||
def _mask_datetimelike_result( | ||
result: Union[np.ndarray, np.datetime64, np.timedelta64], | ||
axis: Optional[int], | ||
mask: Optional[np.ndarray], | ||
orig_values: np.ndarray, | ||
): | ||
if mask is None: | ||
mask = isna(orig_values) | ||
if isinstance(result, np.ndarray): | ||
# we need to apply the mask | ||
result = result.astype("i8").view(orig_values.dtype) | ||
axis_mask = mask.any(axis=axis) | ||
result[axis_mask] = iNaT | ||
else: | ||
if mask.any(): | ||
result = NaT | ||
return result | ||
|
||
|
||
@disallow(PeriodDtype) | ||
|
@@ -544,21 +574,25 @@ def nanmean( | |
>>> nanops.nanmean(s) | ||
1.5 | ||
""" | ||
orig_values = values | ||
|
||
values, mask, dtype, dtype_max, _ = _get_values( | ||
values, skipna, fill_value=0, mask=mask | ||
) | ||
dtype_sum = dtype_max | ||
dtype_count = np.float64 | ||
|
||
# not using needs_i8_conversion because that includes period | ||
if ( | ||
is_integer_dtype(dtype) | ||
or is_datetime64_any_dtype(dtype) | ||
or is_timedelta64_dtype(dtype) | ||
): | ||
datetimelike = False | ||
if dtype.kind in ["m", "M"]: | ||
datetimelike = True | ||
dtype_sum = np.float64 | ||
elif is_integer_dtype(dtype): | ||
dtype_sum = np.float64 | ||
elif is_float_dtype(dtype): | ||
dtype_sum = dtype | ||
dtype_count = dtype | ||
|
||
count = _get_counts(values.shape, mask, axis, dtype=dtype_count) | ||
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum)) | ||
|
||
|
@@ -573,7 +607,10 @@ def nanmean( | |
else: | ||
the_mean = the_sum / count if count > 0 else np.nan | ||
|
||
return _wrap_results(the_mean, dtype) | ||
the_mean = _wrap_results(the_mean, dtype) | ||
if datetimelike and not skipna: | ||
the_mean = _mask_datetimelike_result(the_mean, axis, mask, orig_values) | ||
return the_mean | ||
|
||
|
||
@bottleneck_switch() | ||
|
@@ -639,16 +676,37 @@ def get_median(x): | |
# empty set so return nans of shape "everything but the passed axis" | ||
# since "axis" is where the reduction would occur if we had a nonempty | ||
# array | ||
shp = np.array(values.shape) | ||
dims = np.arange(values.ndim) | ||
ret = np.empty(shp[dims != axis]) | ||
ret.fill(np.nan) | ||
ret = get_empty_reduction_result(values.shape, axis, np.float_, np.nan) | ||
return _wrap_results(ret, dtype) | ||
|
||
# otherwise return a scalar value | ||
return _wrap_results(get_median(values) if notempty else np.nan, dtype) | ||
|
||
|
||
def get_empty_reduction_result( | ||
shape: Tuple[int, ...], axis: int, dtype: np.dtype, fill_value: Any | ||
) -> np.ndarray: | ||
""" | ||
The result from a reduction on an empty ndarray. | ||
Parameters | ||
---------- | ||
shape : Tuple[int] | ||
axis : int | ||
dtype : np.dtype | ||
fill_value : Any | ||
Returns | ||
------- | ||
np.ndarray | ||
""" | ||
shp = np.array(shape) | ||
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. can you add Parametes to the doc-string 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. sure, just pushed |
||
dims = np.arange(len(shape)) | ||
ret = np.empty(shp[dims != axis], dtype=dtype) | ||
ret.fill(fill_value) | ||
return ret | ||
|
||
|
||
def _get_counts_nanvar( | ||
value_counts: Tuple[int], | ||
mask: Optional[np.ndarray], | ||
|
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
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
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.
why don;t you have this take the original values (rather than just the dtype) and compute the mask if needed (e.g. make it optional), rn the caller is responsible for that in multiple places.
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.
if we get to here, we always need the mask
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.
you didn't answer the question. you are adding multiple code blocks which do the same thing; if you are going to consolidate to a function then it makes sense to avoid that yes?
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.
apparently i dont understand the question. IIUC the alternative you're suggesting looks like
and remove the
if mask is None and not skipna: mask = isna(orig_values)
on L516-517. This is 2 lines of code either way, so not a big deal. ill change it if you really care.longer-term this should probably go into
_get_values
, but i want to do that carefully since that may affect other functionsThere 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.
ok i guess - i am thinking u r going to refactor this anyhow as this adds a fair amount of duplication