From 6749864c4531aa08175fe1b4fb6706c9e81ab588 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 16 Oct 2015 18:02:36 -0400 Subject: [PATCH] DEPR: remove some SparsePanel deprecation warnings in testing --- pandas/core/panel.py | 4 +- pandas/sparse/tests/test_sparse.py | 132 +++++++++++++++-------------- pandas/stats/tests/test_moments.py | 3 + pandas/tests/test_frame.py | 12 +-- pandas/tests/test_generic.py | 3 +- pandas/tests/test_multilevel.py | 2 - pandas/tests/test_panel.py | 35 ++++++-- pandas/util/testing.py | 2 - 8 files changed, 107 insertions(+), 86 deletions(-) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 08ef82835830c..da0ab7bc59440 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -680,8 +680,8 @@ def _combine(self, other, func, axis=0): elif np.isscalar(other): return self._combine_const(other, func) else: - raise NotImplementedError(str(type(other)) + - ' is not supported in combine operation with ' + + raise NotImplementedError(str(type(other)) + + ' is not supported in combine operation with ' + str(type(self))) def _combine_const(self, other, func): diff --git a/pandas/sparse/tests/test_sparse.py b/pandas/sparse/tests/test_sparse.py index a86942718091c..9ce08c550dd0d 100644 --- a/pandas/sparse/tests/test_sparse.py +++ b/pandas/sparse/tests/test_sparse.py @@ -39,10 +39,6 @@ from pandas.sparse.tests.test_array import assert_sp_array_equal -import warnings -warnings.filterwarnings(action='ignore', category=FutureWarning) - - def _test_data1(): # nan-based arr = np.arange(20, dtype=float) @@ -503,15 +499,6 @@ def check(a, b): result = self.bseries + self.bseries.to_dense() assert_sp_series_equal(result, self.bseries + self.bseries) - # @dec.knownfailureif(True, 'Known NumPy failer as of 1.5.1') - def test_operators_corner2(self): - raise nose.SkipTest('known failer on numpy 1.5.1') - - # NumPy circumvents __r*__ operations - val = np.float64(3.0) - result = val - self.zbseries - assert_sp_series_equal(result, 3 - self.zbseries) - def test_binary_operators(self): # skipping for now ##### @@ -1778,20 +1765,23 @@ def setUp(self): 'ItemC': panel_data3(), 'ItemD': panel_data1(), } - self.panel = SparsePanel(self.data_dict) + with tm.assert_produces_warning(FutureWarning): + self.panel = SparsePanel(self.data_dict) @staticmethod def _test_op(panel, op): # arithmetic tests - result = op(panel, 1) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + result = op(panel, 1) assert_sp_frame_equal(result['ItemA'], op(panel['ItemA'], 1)) def test_constructor(self): - self.assertRaises(ValueError, SparsePanel, self.data_dict, - items=['Item0', 'ItemA', 'ItemB']) - with tm.assertRaisesRegexp(TypeError, - "input must be a dict, a 'list' was passed"): - SparsePanel(['a', 'b', 'c']) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + self.assertRaises(ValueError, SparsePanel, self.data_dict, + items=['Item0', 'ItemA', 'ItemB']) + with tm.assertRaisesRegexp(TypeError, + "input must be a dict, a 'list' was passed"): + SparsePanel(['a', 'b', 'c']) # deprecation GH11157 def test_deprecation(self): @@ -1800,13 +1790,15 @@ def test_deprecation(self): # GH 9272 def test_constructor_empty(self): - sp = SparsePanel() + with tm.assert_produces_warning(FutureWarning): + sp = SparsePanel() self.assertEqual(len(sp.items), 0) self.assertEqual(len(sp.major_axis), 0) self.assertEqual(len(sp.minor_axis), 0) def test_from_dict(self): - fd = SparsePanel.from_dict(self.data_dict) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + fd = SparsePanel.from_dict(self.data_dict) assert_sp_panel_equal(fd, self.panel) def test_pickle(self): @@ -1830,21 +1822,25 @@ def test_to_dense(self): assert_panel_equal(dwp, dwp2) def test_to_frame(self): - def _compare_with_dense(panel): - slp = panel.to_frame() - dlp = panel.to_dense().to_frame() - self.assert_numpy_array_equal(slp.values, dlp.values) - self.assertTrue(slp.index.equals(dlp.index)) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + + def _compare_with_dense(panel): + slp = panel.to_frame() + dlp = panel.to_dense().to_frame() - _compare_with_dense(self.panel) - _compare_with_dense(self.panel.reindex(items=['ItemA'])) + self.assert_numpy_array_equal(slp.values, dlp.values) + self.assertTrue(slp.index.equals(dlp.index)) - zero_panel = SparsePanel(self.data_dict, default_fill_value=0) - self.assertRaises(Exception, zero_panel.to_frame) + _compare_with_dense(self.panel) + _compare_with_dense(self.panel.reindex(items=['ItemA'])) - self.assertRaises(Exception, self.panel.to_frame, - filter_observations=False) + with tm.assert_produces_warning(FutureWarning): + zero_panel = SparsePanel(self.data_dict, default_fill_value=0) + self.assertRaises(Exception, zero_panel.to_frame) + + self.assertRaises(Exception, self.panel.to_frame, + filter_observations=False) def test_long_to_wide_sparse(self): pass @@ -1885,47 +1881,53 @@ def test_delitem_pop(self): self.assertRaises(KeyError, self.panel.__delitem__, 'ItemC') def test_copy(self): - cop = self.panel.copy() + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + cop = self.panel.copy() assert_sp_panel_equal(cop, self.panel) def test_reindex(self): - def _compare_with_dense(swp, items, major, minor): - swp_re = swp.reindex(items=items, major=major, - minor=minor) - dwp_re = swp.to_dense().reindex(items=items, major=major, - minor=minor) - assert_panel_equal(swp_re.to_dense(), dwp_re) - - _compare_with_dense(self.panel, self.panel.items[:2], - self.panel.major_axis[::2], - self.panel.minor_axis[::2]) - _compare_with_dense(self.panel, None, - self.panel.major_axis[::2], - self.panel.minor_axis[::2]) - - self.assertRaises(ValueError, self.panel.reindex) - - # TODO: do something about this later... - self.assertRaises(Exception, self.panel.reindex, - items=['item0', 'ItemA', 'ItemB']) - - # test copying - cp = self.panel.reindex(self.panel.major_axis, copy=True) - cp['ItemA']['E'] = cp['ItemA']['A'] - self.assertNotIn('E', self.panel['ItemA']) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + + def _compare_with_dense(swp, items, major, minor): + swp_re = swp.reindex(items=items, major=major, + minor=minor) + dwp_re = swp.to_dense().reindex(items=items, major=major, + minor=minor) + assert_panel_equal(swp_re.to_dense(), dwp_re) + + _compare_with_dense(self.panel, self.panel.items[:2], + self.panel.major_axis[::2], + self.panel.minor_axis[::2]) + _compare_with_dense(self.panel, None, + self.panel.major_axis[::2], + self.panel.minor_axis[::2]) + + self.assertRaises(ValueError, self.panel.reindex) + + # TODO: do something about this later... + self.assertRaises(Exception, self.panel.reindex, + items=['item0', 'ItemA', 'ItemB']) + + # test copying + cp = self.panel.reindex(self.panel.major_axis, copy=True) + cp['ItemA']['E'] = cp['ItemA']['A'] + self.assertNotIn('E', self.panel['ItemA']) def test_operators(self): def _check_ops(panel): + def _dense_comp(op): - dense = panel.to_dense() - sparse_result = op(panel) - dense_result = op(dense) - assert_panel_equal(sparse_result.to_dense(), dense_result) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + dense = panel.to_dense() + sparse_result = op(panel) + dense_result = op(dense) + assert_panel_equal(sparse_result.to_dense(), dense_result) def _mixed_comp(op): - result = op(panel, panel.to_dense()) - expected = op(panel.to_dense(), panel.to_dense()) - assert_panel_equal(result, expected) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + result = op(panel, panel.to_dense()) + expected = op(panel.to_dense(), panel.to_dense()) + assert_panel_equal(result, expected) op1 = lambda x: x + 2 diff --git a/pandas/stats/tests/test_moments.py b/pandas/stats/tests/test_moments.py index 3615cc3dc8ad8..86c8f5298e0ab 100644 --- a/pandas/stats/tests/test_moments.py +++ b/pandas/stats/tests/test_moments.py @@ -45,6 +45,9 @@ def setUp(self): self._create_data() warnings.simplefilter("ignore", category=FutureWarning) + def tearDown(self): + warnings.simplefilter("default", category=FutureWarning) + def test_centered_axis_validation(self): # ok mom.rolling_mean(Series(np.ones(10)),3,center=True ,axis=0) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 13c671e8e4e59..a45f4bf1726f2 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -381,15 +381,11 @@ def test_getitem_boolean(self): assert_frame_equal(subframe_obj, subframe) # test that Series indexers reindex - import warnings - warnings.filterwarnings(action='ignore', category=UserWarning) - - indexer_obj = indexer_obj.reindex(self.tsframe.index[::-1]) - - subframe_obj = self.tsframe[indexer_obj] - assert_frame_equal(subframe_obj, subframe) + with tm.assert_produces_warning(UserWarning): + indexer_obj = indexer_obj.reindex(self.tsframe.index[::-1]) - warnings.filterwarnings(action='default', category=UserWarning) + subframe_obj = self.tsframe[indexer_obj] + assert_frame_equal(subframe_obj, subframe) # test df[df > 0] for df in [ self.tsframe, self.mixed_frame, self.mixed_float, self.mixed_int ]: diff --git a/pandas/tests/test_generic.py b/pandas/tests/test_generic.py index 061382e0e16de..d29673e96ecdd 100644 --- a/pandas/tests/test_generic.py +++ b/pandas/tests/test_generic.py @@ -39,8 +39,7 @@ class Generic(object): _multiprocess_can_split_ = True def setUp(self): - import warnings - warnings.filterwarnings(action='ignore', category=FutureWarning) + pass @property def _ndim(self): diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index df61387734cb3..5b00ea163d85f 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -28,8 +28,6 @@ class TestMultiLevel(tm.TestCase): _multiprocess_can_split_ = True def setUp(self): - import warnings - warnings.filterwarnings(action='ignore', category=FutureWarning) index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], ['one', 'two', 'three']], diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 0dad55a9133b6..1f8bcf8c9879f 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -5,6 +5,7 @@ from inspect import getargspec import operator import nose +from functools import wraps import numpy as np import pandas as pd @@ -17,6 +18,7 @@ import pandas.core.common as com from pandas import compat from pandas.compat import range, lrange, StringIO, OrderedDict +from pandas import SparsePanel from pandas.util.testing import (assert_panel_equal, assert_frame_equal, @@ -31,6 +33,22 @@ import pandas.core.panel as panelm import pandas.util.testing as tm +def ignore_sparse_panel_future_warning(func): + """ + decorator to ignore FutureWarning if we have a SparsePanel + + can be removed when SparsePanel is fully removed + """ + @wraps(func) + def wrapper(self, *args, **kwargs): + + if isinstance(self.panel, SparsePanel): + with assert_produces_warning(FutureWarning, check_stacklevel=False): + return func(self, *args, **kwargs) + else: + return func(self, *args, **kwargs) + + return wrapper class PanelTests(object): panel = None @@ -56,6 +74,7 @@ class SafeForLongAndSparse(object): def test_repr(self): foo = repr(self.panel) + @ignore_sparse_panel_future_warning def test_copy_names(self): for attr in ('major_axis', 'minor_axis'): getattr(self.panel, attr).name = None @@ -233,6 +252,7 @@ def test_get_plane_axes(self): index, columns = self.panel._get_plane_axes('minor_axis') index, columns = self.panel._get_plane_axes(0) + @ignore_sparse_panel_future_warning def test_truncate(self): dates = self.panel.major_axis start, end = dates[1], dates[5] @@ -293,6 +313,7 @@ def test_iteritems(self): self.assertEqual(len(list(compat.iteritems(self.panel))), len(self.panel.items)) + @ignore_sparse_panel_future_warning def test_combineFrame(self): def check_op(op, name): # items @@ -321,7 +342,7 @@ def check_op(op, name): assert_frame_equal(result.minor_xs(idx), op(self.panel.minor_xs(idx), xs)) - from pandas import SparsePanel + ops = ['add', 'sub', 'mul', 'truediv', 'floordiv'] if not compat.PY3: ops.append('div') @@ -348,16 +369,18 @@ def check_op(op, name): com.pprint_thing("Failing operation: %r" % name) raise + @ignore_sparse_panel_future_warning def test_combinePanel(self): result = self.panel.add(self.panel) self.assert_panel_equal(result, self.panel * 2) + @ignore_sparse_panel_future_warning def test_neg(self): self.assert_panel_equal(-self.panel, self.panel * -1) # issue 7692 def test_raise_when_not_implemented(self): - p = Panel(np.arange(3*4*5).reshape(3,4,5), items=['ItemA','ItemB','ItemC'], + p = Panel(np.arange(3*4*5).reshape(3,4,5), items=['ItemA','ItemB','ItemC'], major_axis=pd.date_range('20130101',periods=4),minor_axis=list('ABCDE')) d = p.sum(axis=1).ix[0] ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'div', 'mod', 'pow'] @@ -365,6 +388,7 @@ def test_raise_when_not_implemented(self): with self.assertRaises(NotImplementedError): getattr(p,op)(d, axis=0) + @ignore_sparse_panel_future_warning def test_select(self): p = self.panel @@ -396,7 +420,9 @@ def test_get_value(self): expected = self.panel[item][mnr][mjr] assert_almost_equal(result, expected) + @ignore_sparse_panel_future_warning def test_abs(self): + result = self.panel.abs() result2 = abs(self.panel) expected = np.abs(self.panel) @@ -872,9 +898,6 @@ def assert_panel_equal(cls, x, y): assert_panel_equal(x, y) def setUp(self): - import warnings - warnings.filterwarnings(action='ignore', category=FutureWarning) - self.panel = _panel.copy() self.panel.major_axis.name = None self.panel.minor_axis.name = None @@ -1534,6 +1557,7 @@ def test_transpose_copy(self): panel.values[0, 1, 1] = np.nan self.assertTrue(notnull(result.values[1, 0, 1])) + @ignore_sparse_panel_future_warning def test_to_frame(self): # filtered filtered = self.panel.to_frame() @@ -2313,6 +2337,7 @@ def test_to_string(self): buf = StringIO() self.panel.to_string(buf) + @ignore_sparse_panel_future_warning def test_truncate(self): dates = self.panel.index.levels[0] start, end = dates[1], dates[5] diff --git a/pandas/util/testing.py b/pandas/util/testing.py index df3f1aaa815fa..d142ffdbad983 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -59,7 +59,6 @@ def reset_testing_mode(): if 'deprecate' in testing_mode: warnings.simplefilter('ignore', DeprecationWarning) - set_testing_mode() class TestCase(unittest.TestCase): @@ -1975,7 +1974,6 @@ def handle_success(self, exc_type, exc_value, traceback): raise_with_traceback(e, traceback) return True - @contextmanager def assert_produces_warning(expected_warning=Warning, filter_level="always", clear=None, check_stacklevel=True):