-
-
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
Changes from 1 commit
854da9e
f1c536c
f2d1e7f
58182aa
1c5bb0f
482ac89
1004ca6
3987063
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,40 @@ 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 | ||
if mask is None and not skipna: | ||
mask = isna(orig_values) | ||
|
||
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.dtype) | ||
return the_sum | ||
|
||
|
||
def mask_datetimelike_result(result, axis, mask, orig_dtype): | ||
if isinstance(result, np.ndarray): | ||
# we need to apply the mask | ||
result = result.astype("i8").view(orig_dtype) | ||
axis_mask = mask.any(axis=axis) | ||
result[axis_mask] = iNaT | ||
else: | ||
if mask.any(): | ||
result = NaT | ||
return result | ||
|
||
|
||
@disallow(PeriodDtype) | ||
|
@@ -544,21 +569,27 @@ 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 | ||
if mask is None and not skipna: | ||
mask = isna(orig_values) | ||
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 +604,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.dtype) | ||
return the_mean | ||
|
||
|
||
@bottleneck_switch() | ||
|
@@ -639,16 +673,24 @@ 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, axis: int, dtype, fill_value) -> np.ndarray: | ||
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 type dtype, fill_value |
||
""" | ||
The result from a reduction on an empty 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], | ||
|
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.
can you type
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.
will do