-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 4 commits
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,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 | ||
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: Union[np.ndarray, np.datetime64, np.timedelta64], | ||
axis: Optional[int], | ||
mask: np.ndarray, | ||
orig_dtype: np.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 +574,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 +609,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 +678,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.
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