Skip to content

Commit 40a91c5

Browse files
jrebackjavadnoorb
authored andcommitted
TST: test_nanops some parametrize & catch warnings (RuntimeWarning: All-Nan slice in tests) (pandas-dev#20484)
* TST: test_nanops some parametrize & catch warnings (RuntimeWarning: All-NaN slice in tests) * COMPAT: work around deprecation warning on non-equal dtype comparisons closes pandas-dev#20011 * WARN: bincount minlength deprecation warning
1 parent daa3b33 commit 40a91c5

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3860,7 +3860,7 @@ def count(self):
38603860

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

38653865
return Series(out,
38663866
index=self.grouper.result_index,

pandas/core/indexes/base.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,14 @@ def cmp_method(self, other):
9999
# don't pass MultiIndex
100100
with np.errstate(all='ignore'):
101101
result = ops._comp_method_OBJECT_ARRAY(op, self.values, other)
102+
102103
else:
103-
with np.errstate(all='ignore'):
104-
result = op(self.values, np.asarray(other))
104+
105+
# numpy will show a DeprecationWarning on invalid elementwise
106+
# comparisons, this will raise in the future
107+
with warnings.catch_warnings(record=True):
108+
with np.errstate(all='ignore'):
109+
result = op(self.values, np.asarray(other))
105110

106111
# technically we could support bool dtyped Index
107112
# for now just return the indexing array directly

pandas/tests/frame/test_analytics.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ def wrapper(x):
529529
self._check_stat_op('median', wrapper, check_dates=True)
530530

531531
def test_min(self):
532-
self._check_stat_op('min', np.min, check_dates=True)
532+
with warnings.catch_warnings(record=True):
533+
self._check_stat_op('min', np.min, check_dates=True)
533534
self._check_stat_op('min', np.min, frame=self.intframe)
534535

535536
def test_cummin(self):
@@ -579,7 +580,8 @@ def test_cummax(self):
579580
assert np.shape(cummax_xs) == np.shape(self.tsframe)
580581

581582
def test_max(self):
582-
self._check_stat_op('max', np.max, check_dates=True)
583+
with warnings.catch_warnings(record=True):
584+
self._check_stat_op('max', np.max, check_dates=True)
583585
self._check_stat_op('max', np.max, frame=self.intframe)
584586

585587
def test_mad(self):

pandas/tests/test_nanops.py

+32-43
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,6 @@ def check_funs(self, testfunc, targfunc, allow_complex=True,
301301
allow_complex=allow_complex)
302302
self.check_fun(testfunc, targfunc, 'arr_obj', **kwargs)
303303

304-
def check_funs_ddof(self,
305-
testfunc,
306-
targfunc,
307-
allow_complex=True,
308-
allow_all_nan=True,
309-
allow_str=True,
310-
allow_date=False,
311-
allow_tdelta=False,
312-
allow_obj=True, ):
313-
for ddof in range(3):
314-
try:
315-
self.check_funs(testfunc, targfunc, allow_complex,
316-
allow_all_nan, allow_str, allow_date,
317-
allow_tdelta, allow_obj, ddof=ddof)
318-
except BaseException as exc:
319-
exc.args += ('ddof %s' % ddof, )
320-
raise
321-
322304
def _badobj_wrap(self, value, func, allow_complex=True, **kwargs):
323305
if value.dtype.kind == 'O':
324306
if allow_complex:
@@ -381,37 +363,46 @@ def test_nanmedian(self):
381363
allow_str=False, allow_date=False,
382364
allow_tdelta=True, allow_obj='convert')
383365

384-
def test_nanvar(self):
385-
self.check_funs_ddof(nanops.nanvar, np.var, allow_complex=False,
386-
allow_str=False, allow_date=False,
387-
allow_tdelta=True, allow_obj='convert')
366+
@pytest.mark.parametrize('ddof', range(3))
367+
def test_nanvar(self, ddof):
368+
self.check_funs(nanops.nanvar, np.var, allow_complex=False,
369+
allow_str=False, allow_date=False,
370+
allow_tdelta=True, allow_obj='convert', ddof=ddof)
388371

389-
def test_nanstd(self):
390-
self.check_funs_ddof(nanops.nanstd, np.std, allow_complex=False,
391-
allow_str=False, allow_date=False,
392-
allow_tdelta=True, allow_obj='convert')
372+
@pytest.mark.parametrize('ddof', range(3))
373+
def test_nanstd(self, ddof):
374+
self.check_funs(nanops.nanstd, np.std, allow_complex=False,
375+
allow_str=False, allow_date=False,
376+
allow_tdelta=True, allow_obj='convert', ddof=ddof)
393377

394378
@td.skip_if_no('scipy', min_version='0.17.0')
395-
def test_nansem(self):
379+
@pytest.mark.parametrize('ddof', range(3))
380+
def test_nansem(self, ddof):
396381
from scipy.stats import sem
397382
with np.errstate(invalid='ignore'):
398-
self.check_funs_ddof(nanops.nansem, sem, allow_complex=False,
399-
allow_str=False, allow_date=False,
400-
allow_tdelta=False, allow_obj='convert')
383+
self.check_funs(nanops.nansem, sem, allow_complex=False,
384+
allow_str=False, allow_date=False,
385+
allow_tdelta=False, allow_obj='convert', ddof=ddof)
401386

402387
def _minmax_wrap(self, value, axis=None, func=None):
388+
389+
# numpy warns if all nan
403390
res = func(value, axis)
404391
if res.dtype.kind == 'm':
405392
res = np.atleast_1d(res)
406393
return res
407394

408395
def test_nanmin(self):
409-
func = partial(self._minmax_wrap, func=np.min)
410-
self.check_funs(nanops.nanmin, func, allow_str=False, allow_obj=False)
396+
with warnings.catch_warnings(record=True):
397+
func = partial(self._minmax_wrap, func=np.min)
398+
self.check_funs(nanops.nanmin, func,
399+
allow_str=False, allow_obj=False)
411400

412401
def test_nanmax(self):
413-
func = partial(self._minmax_wrap, func=np.max)
414-
self.check_funs(nanops.nanmax, func, allow_str=False, allow_obj=False)
402+
with warnings.catch_warnings(record=True):
403+
func = partial(self._minmax_wrap, func=np.max)
404+
self.check_funs(nanops.nanmax, func,
405+
allow_str=False, allow_obj=False)
415406

416407
def _argminmax_wrap(self, value, axis=None, func=None):
417408
res = func(value, axis)
@@ -425,17 +416,15 @@ def _argminmax_wrap(self, value, axis=None, func=None):
425416
return res
426417

427418
def test_nanargmax(self):
428-
func = partial(self._argminmax_wrap, func=np.argmax)
429-
self.check_funs(nanops.nanargmax, func, allow_str=False,
430-
allow_obj=False, allow_date=True, allow_tdelta=True)
419+
with warnings.catch_warnings(record=True):
420+
func = partial(self._argminmax_wrap, func=np.argmax)
421+
self.check_funs(nanops.nanargmax, func,
422+
allow_str=False, allow_obj=False,
423+
allow_date=True, allow_tdelta=True)
431424

432425
def test_nanargmin(self):
433-
func = partial(self._argminmax_wrap, func=np.argmin)
434-
if tm.sys.version_info[0:2] == (2, 6):
435-
self.check_funs(nanops.nanargmin, func, allow_date=True,
436-
allow_tdelta=True, allow_str=False,
437-
allow_obj=False)
438-
else:
426+
with warnings.catch_warnings(record=True):
427+
func = partial(self._argminmax_wrap, func=np.argmin)
439428
self.check_funs(nanops.nanargmin, func, allow_str=False,
440429
allow_obj=False)
441430

pandas/tests/test_panel.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ def wrapper(x):
100100
self._check_stat_op('median', wrapper)
101101

102102
def test_min(self):
103-
self._check_stat_op('min', np.min)
103+
with catch_warnings(record=True):
104+
self._check_stat_op('min', np.min)
104105

105106
def test_max(self):
106-
self._check_stat_op('max', np.max)
107+
with catch_warnings(record=True):
108+
self._check_stat_op('max', np.max)
107109

108110
@td.skip_if_no_scipy
109111
def test_skew(self):

0 commit comments

Comments
 (0)