-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: datetime64 series reduces to nan when empty instead of nat #11245
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 all commits
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 |
---|---|---|
|
@@ -425,65 +425,34 @@ def nansem(values, axis=None, skipna=True, ddof=1): | |
return np.sqrt(var) / np.sqrt(count) | ||
|
||
|
||
@bottleneck_switch() | ||
def nanmin(values, axis=None, skipna=True): | ||
values, mask, dtype, dtype_max = _get_values(values, skipna, | ||
fill_value_typ='+inf') | ||
|
||
# numpy 1.6.1 workaround in Python 3.x | ||
if is_object_dtype(values) and compat.PY3: | ||
if values.ndim > 1: | ||
apply_ax = axis if axis is not None else 0 | ||
result = np.apply_along_axis(builtins.min, apply_ax, values) | ||
else: | ||
try: | ||
result = builtins.min(values) | ||
except: | ||
result = np.nan | ||
else: | ||
def _nanminmax(meth, fill_value_typ): | ||
@bottleneck_switch() | ||
def reduction(values, axis=None, skipna=True): | ||
values, mask, dtype, dtype_max = _get_values( | ||
values, | ||
skipna, | ||
fill_value_typ=fill_value_typ, | ||
) | ||
|
||
if ((axis is not None and values.shape[axis] == 0) | ||
or values.size == 0): | ||
try: | ||
result = ensure_float(values.sum(axis, dtype=dtype_max)) | ||
result = getattr(values, meth)(axis, dtype=dtype_max) | ||
result.fill(np.nan) | ||
except: | ||
result = np.nan | ||
else: | ||
result = values.min(axis) | ||
result = getattr(values, meth)(axis) | ||
|
||
result = _wrap_results(result, dtype) | ||
return _maybe_null_out(result, axis, mask) | ||
result = _wrap_results(result, dtype) | ||
return _maybe_null_out(result, axis, mask) | ||
|
||
reduction.__name__ = 'nan' + meth | ||
return reduction | ||
|
||
@bottleneck_switch() | ||
def nanmax(values, axis=None, skipna=True): | ||
values, mask, dtype, dtype_max = _get_values(values, skipna, | ||
fill_value_typ='-inf') | ||
|
||
# numpy 1.6.1 workaround in Python 3.x | ||
if is_object_dtype(values) and compat.PY3: | ||
|
||
if values.ndim > 1: | ||
apply_ax = axis if axis is not None else 0 | ||
result = np.apply_along_axis(builtins.max, apply_ax, values) | ||
else: | ||
try: | ||
result = builtins.max(values) | ||
except: | ||
result = np.nan | ||
else: | ||
if ((axis is not None and values.shape[axis] == 0) | ||
or values.size == 0): | ||
try: | ||
result = ensure_float(values.sum(axis, dtype=dtype_max)) | ||
result.fill(np.nan) | ||
except: | ||
result = np.nan | ||
else: | ||
result = values.max(axis) | ||
|
||
result = _wrap_results(result, dtype) | ||
return _maybe_null_out(result, axis, mask) | ||
nanmin = _nanminmax('min', fill_value_typ='+inf') | ||
nanmax = _nanminmax('max', fill_value_typ='-inf') | ||
|
||
|
||
def nanargmax(values, axis=None, skipna=True): | ||
|
@@ -637,7 +606,7 @@ def _maybe_null_out(result, axis, mask): | |
else: | ||
result = result.astype('f8') | ||
result[null_mask] = np.nan | ||
else: | ||
elif result is not tslib.NaT: | ||
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. if we started with 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. so this should be 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. == tslib.iNaT but puzzled why a NaT is there 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. If you take a ts and take a view as an int and then reduce, I think you still want 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. Oh, I see what you are saying, you were suggesting: elif result.view('i8') == tslib.iNaT It looks like result is still just a timestamp at this point so it will be the 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. no I mean result should already be an int if it's M8 as wrapping is the last step 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. ok then |
||
null_mask = mask.size - mask.sum() | ||
if null_mask == 0: | ||
result = np.nan | ||
|
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.
We have already wrapped the types by the time we call maybe_null_out. The result will already be coerced so I think the
is
check is safe.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.
ok, then your check is good. thxs