Skip to content

FIX: Scalar timedelta NaT results raise #7661

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def _wrap_results(result, dtype):
from pandas import Series

# coerce float to results
if is_float(result):
if isnull(result):
result = tslib.NaT
elif is_float(result):
result = int(result)
result = Series([result], dtype='timedelta64[ns]')
else:
Expand Down
25 changes: 15 additions & 10 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from pandas.core.common import isnull
from pandas.tslib import iNaT
import pandas.core.nanops as nanops
import pandas.util.testing as tm

Expand All @@ -30,6 +31,8 @@ def setUp(self):
self.arr_shape).astype('m8[ns]')

self.arr_nan = np.tile(np.nan, self.arr_shape)
self.arr_datenat = np.tile(iNaT, self.arr_shape).astype('M8[ns]')
self.arr_tdeltanat = np.tile(iNaT, self.arr_shape).astype('m8[ns]')
self.arr_float_nan = np.vstack([self.arr_float, self.arr_nan])
self.arr_float1_nan = np.vstack([self.arr_float1, self.arr_nan])
self.arr_nan_float1 = np.vstack([self.arr_nan, self.arr_float1])
Expand Down Expand Up @@ -244,13 +247,19 @@ def check_funs(self, testfunc, targfunc,
else:
self.check_fun(testfunc, targfunc, 'arr_date', **kwargs)
objs += [self.arr_date.astype('O')]
if allow_all_nan:
self.check_fun(testfunc, targfunc, 'arr_datenat',
**kwargs)
try:
targfunc(self.arr_tdelta)
except TypeError:
pass
else:
self.check_fun(testfunc, targfunc, 'arr_tdelta', **kwargs)
objs += [self.arr_tdelta.astype('O')]
objs += [self.arr_date.astype('O')]
if allow_all_nan:
self.check_fun(testfunc, targfunc, 'arr_tdeltanat',
**kwargs)

if allow_obj:
self.arr_obj = np.vstack(objs)
Expand Down Expand Up @@ -291,18 +300,15 @@ def test_nanall(self):
allow_all_nan=False, allow_str=False, allow_date=False)

def test_nansum(self):
self.check_funs(nanops.nansum, np.sum,
allow_str=False, allow_date=False)
self.check_funs(nanops.nansum, np.sum, allow_str=False)

def test_nanmean(self):
self.check_funs(nanops.nanmean, np.mean,
allow_complex=False, allow_obj=False,
allow_str=False, allow_date=False)
self.check_funs(nanops.nanmean, np.mean, allow_complex=False,
allow_obj=False, allow_str=False)

def test_nanmedian(self):
self.check_funs(nanops.nanmedian, np.median,
allow_complex=False, allow_str=False, allow_date=False,
allow_obj='convert')
self.check_funs(nanops.nanmedian, np.median, allow_complex=False,
allow_str=False, allow_obj='convert')

def test_nanvar(self):
self.check_funs_ddof(nanops.nanvar, np.var,
Expand Down Expand Up @@ -349,7 +355,6 @@ def test_nanargmin(self):
func = partial(self._argminmax_wrap, func=np.argmin)
if tm.sys.version_info[0:2] == (2, 6):
self.check_funs(nanops.nanargmin, func,
allow_date=False,
allow_str=False, allow_obj=False)
else:
self.check_funs(nanops.nanargmin, func,
Expand Down