Skip to content

TST: test_nanops some parametrize & catch warnings (RuntimeWarning: All-Nan slice in tests) #20484

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 3 commits into from
Mar 26, 2018
Merged
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
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3860,7 +3860,7 @@ def count(self):

mask = (ids != -1) & ~isna(val)
ids = _ensure_platform_int(ids)
out = np.bincount(ids[mask], minlength=ngroups or None)
out = np.bincount(ids[mask], minlength=ngroups or 0)

return Series(out,
index=self.grouper.result_index,
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ def cmp_method(self, other):
# don't pass MultiIndex
with np.errstate(all='ignore'):
result = ops._comp_method_OBJECT_ARRAY(op, self.values, other)

else:
with np.errstate(all='ignore'):
result = op(self.values, np.asarray(other))

# numpy will show a DeprecationWarning on invalid elementwise
# comparisons, this will raise in the future
with warnings.catch_warnings(record=True):
with np.errstate(all='ignore'):
result = op(self.values, np.asarray(other))

# technically we could support bool dtyped Index
# for now just return the indexing array directly
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ def wrapper(x):
self._check_stat_op('median', wrapper, check_dates=True)

def test_min(self):
self._check_stat_op('min', np.min, check_dates=True)
with warnings.catch_warnings(record=True):
self._check_stat_op('min', np.min, check_dates=True)
self._check_stat_op('min', np.min, frame=self.intframe)

def test_cummin(self):
Expand Down Expand Up @@ -579,7 +580,8 @@ def test_cummax(self):
assert np.shape(cummax_xs) == np.shape(self.tsframe)

def test_max(self):
self._check_stat_op('max', np.max, check_dates=True)
with warnings.catch_warnings(record=True):
self._check_stat_op('max', np.max, check_dates=True)
self._check_stat_op('max', np.max, frame=self.intframe)

def test_mad(self):
Expand Down
75 changes: 32 additions & 43 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,24 +301,6 @@ def check_funs(self, testfunc, targfunc, allow_complex=True,
allow_complex=allow_complex)
self.check_fun(testfunc, targfunc, 'arr_obj', **kwargs)

def check_funs_ddof(self,
testfunc,
targfunc,
allow_complex=True,
allow_all_nan=True,
allow_str=True,
allow_date=False,
allow_tdelta=False,
allow_obj=True, ):
for ddof in range(3):
try:
self.check_funs(testfunc, targfunc, allow_complex,
allow_all_nan, allow_str, allow_date,
allow_tdelta, allow_obj, ddof=ddof)
except BaseException as exc:
exc.args += ('ddof %s' % ddof, )
raise

def _badobj_wrap(self, value, func, allow_complex=True, **kwargs):
if value.dtype.kind == 'O':
if allow_complex:
Expand Down Expand Up @@ -381,37 +363,46 @@ def test_nanmedian(self):
allow_str=False, allow_date=False,
allow_tdelta=True, allow_obj='convert')

def test_nanvar(self):
self.check_funs_ddof(nanops.nanvar, np.var, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=True, allow_obj='convert')
@pytest.mark.parametrize('ddof', range(3))
def test_nanvar(self, ddof):
self.check_funs(nanops.nanvar, np.var, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=True, allow_obj='convert', ddof=ddof)

def test_nanstd(self):
self.check_funs_ddof(nanops.nanstd, np.std, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=True, allow_obj='convert')
@pytest.mark.parametrize('ddof', range(3))
def test_nanstd(self, ddof):
self.check_funs(nanops.nanstd, np.std, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=True, allow_obj='convert', ddof=ddof)

@td.skip_if_no('scipy', min_version='0.17.0')
def test_nansem(self):
@pytest.mark.parametrize('ddof', range(3))
def test_nansem(self, ddof):
from scipy.stats import sem
with np.errstate(invalid='ignore'):
self.check_funs_ddof(nanops.nansem, sem, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=False, allow_obj='convert')
self.check_funs(nanops.nansem, sem, allow_complex=False,
allow_str=False, allow_date=False,
allow_tdelta=False, allow_obj='convert', ddof=ddof)

def _minmax_wrap(self, value, axis=None, func=None):

# numpy warns if all nan
res = func(value, axis)
if res.dtype.kind == 'm':
res = np.atleast_1d(res)
return res

def test_nanmin(self):
func = partial(self._minmax_wrap, func=np.min)
self.check_funs(nanops.nanmin, func, allow_str=False, allow_obj=False)
with warnings.catch_warnings(record=True):
func = partial(self._minmax_wrap, func=np.min)
self.check_funs(nanops.nanmin, func,
allow_str=False, allow_obj=False)

def test_nanmax(self):
func = partial(self._minmax_wrap, func=np.max)
self.check_funs(nanops.nanmax, func, allow_str=False, allow_obj=False)
with warnings.catch_warnings(record=True):
func = partial(self._minmax_wrap, func=np.max)
self.check_funs(nanops.nanmax, func,
allow_str=False, allow_obj=False)

def _argminmax_wrap(self, value, axis=None, func=None):
res = func(value, axis)
Expand All @@ -425,17 +416,15 @@ def _argminmax_wrap(self, value, axis=None, func=None):
return res

def test_nanargmax(self):
func = partial(self._argminmax_wrap, func=np.argmax)
self.check_funs(nanops.nanargmax, func, allow_str=False,
allow_obj=False, allow_date=True, allow_tdelta=True)
with warnings.catch_warnings(record=True):
func = partial(self._argminmax_wrap, func=np.argmax)
self.check_funs(nanops.nanargmax, func,
allow_str=False, allow_obj=False,
allow_date=True, allow_tdelta=True)

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=True,
allow_tdelta=True, allow_str=False,
allow_obj=False)
else:
with warnings.catch_warnings(record=True):
func = partial(self._argminmax_wrap, func=np.argmin)
self.check_funs(nanops.nanargmin, func, allow_str=False,
allow_obj=False)

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ def wrapper(x):
self._check_stat_op('median', wrapper)

def test_min(self):
self._check_stat_op('min', np.min)
with catch_warnings(record=True):
self._check_stat_op('min', np.min)

def test_max(self):
self._check_stat_op('max', np.max)
with catch_warnings(record=True):
self._check_stat_op('max', np.max)

@td.skip_if_no_scipy
def test_skew(self):
Expand Down