From f8506e2c1ccda1fe774e0935b0271d05c21072f8 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 8 Feb 2019 19:59:45 -0800 Subject: [PATCH 1/2] DEPR: remove assert_panel_equal --- pandas/tests/generic/test_generic.py | 32 +- pandas/tests/generic/test_panel.py | 38 -- pandas/tests/indexing/common.py | 2 - .../tests/indexing/multiindex/test_panel.py | 46 --- pandas/tests/indexing/test_panel.py | 90 +---- pandas/tests/indexing/test_partial.py | 32 +- .../tests/io/generate_legacy_storage_files.py | 16 +- pandas/tests/io/test_packers.py | 31 +- pandas/tests/io/test_pickle.py | 4 + pandas/tests/test_expressions.py | 53 +-- pandas/tests/test_multilevel.py | 14 +- pandas/tests/test_panel.py | 358 +----------------- pandas/util/testing.py | 63 --- 13 files changed, 27 insertions(+), 752 deletions(-) delete mode 100644 pandas/tests/generic/test_panel.py diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index fb17b47948336..c2f6cbf4c564c 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -14,8 +14,7 @@ import pandas as pd from pandas import DataFrame, MultiIndex, Panel, Series, date_range import pandas.util.testing as tm -from pandas.util.testing import ( - assert_frame_equal, assert_panel_equal, assert_series_equal) +from pandas.util.testing import assert_frame_equal, assert_series_equal import pandas.io.formats.printing as printing @@ -701,16 +700,9 @@ def test_sample(sel): assert_frame_equal(sample1, df[['colString']]) # Test default axes - with catch_warnings(record=True): - simplefilter("ignore", FutureWarning) - p = Panel(items=['a', 'b', 'c'], major_axis=[2, 4, 6], - minor_axis=[1, 3, 5]) - assert_panel_equal( - p.sample(n=3, random_state=42), p.sample(n=3, axis=1, - random_state=42)) - assert_frame_equal( - df.sample(n=3, random_state=42), df.sample(n=3, axis=0, - random_state=42)) + assert_frame_equal( + df.sample(n=3, random_state=42), df.sample(n=3, axis=0, + random_state=42)) # Test that function aligns weights with frame df = DataFrame( @@ -950,22 +942,6 @@ def test_pipe_tuple_error(self): with pytest.raises(ValueError): df.A.pipe((f, 'y'), x=1, y=0) - def test_pipe_panel(self): - with catch_warnings(record=True): - simplefilter("ignore", FutureWarning) - wp = Panel({'r1': DataFrame({"A": [1, 2, 3]})}) - f = lambda x, y: x + y - result = wp.pipe(f, 2) - expected = wp + 2 - assert_panel_equal(result, expected) - - result = wp.pipe((f, 'y'), x=1) - expected = wp + 1 - assert_panel_equal(result, expected) - - with pytest.raises(ValueError): - wp.pipe((f, 'y'), x=1, y=1) - @pytest.mark.parametrize('box', [pd.Series, pd.DataFrame]) def test_axis_classmethods(self, box): obj = box() diff --git a/pandas/tests/generic/test_panel.py b/pandas/tests/generic/test_panel.py deleted file mode 100644 index 73b8798661011..0000000000000 --- a/pandas/tests/generic/test_panel.py +++ /dev/null @@ -1,38 +0,0 @@ -# -*- coding: utf-8 -*- -# pylint: disable-msg=E1101,W0612 - -from warnings import catch_warnings, simplefilter - -from pandas import Panel -from pandas.util.testing import assert_panel_equal - -from .test_generic import Generic - - -class TestPanel(Generic): - _typ = Panel - _comparator = lambda self, x, y: assert_panel_equal(x, y, by_blocks=True) - - -# run all the tests, but wrap each in a warning catcher -for t in ['test_rename', 'test_get_numeric_data', - 'test_get_default', 'test_nonzero', - 'test_downcast', 'test_constructor_compound_dtypes', - 'test_head_tail', - 'test_size_compat', 'test_split_compat', - 'test_unexpected_keyword', - 'test_stat_unexpected_keyword', 'test_api_compat', - 'test_stat_non_defaults_args', - 'test_truncate_out_of_bounds', - 'test_metadata_propagation', 'test_copy_and_deepcopy', - 'test_pct_change', 'test_sample']: - - def f(): - def tester(self): - f = getattr(super(TestPanel, self), t) - with catch_warnings(record=True): - simplefilter("ignore", FutureWarning) - f() - return tester - - setattr(TestPanel, t, f()) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index f4d6fe428515e..91ea38920c702 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -233,8 +233,6 @@ def _print(result, error=None): tm.assert_series_equal(rs, xp) elif xp.ndim == 2: tm.assert_frame_equal(rs, xp) - elif xp.ndim == 3: - tm.assert_panel_equal(rs, xp) result = 'ok' except AssertionError as e: detail = str(e) diff --git a/pandas/tests/indexing/multiindex/test_panel.py b/pandas/tests/indexing/multiindex/test_panel.py index 68c8fadd2f0dd..314009146911a 100644 --- a/pandas/tests/indexing/multiindex/test_panel.py +++ b/pandas/tests/indexing/multiindex/test_panel.py @@ -55,49 +55,3 @@ def test_iloc_getitem_panel_multiindex(self): result = p.loc[:, (1, 'y'), 'u'] tm.assert_series_equal(result, expected) - - def test_panel_setitem_with_multiindex(self): - - # 10360 - # failing with a multi-index - arr = np.array([[[1, 2, 3], [0, 0, 0]], - [[0, 0, 0], [0, 0, 0]]], - dtype=np.float64) - - # reg index - axes = dict(items=['A', 'B'], major_axis=[0, 1], - minor_axis=['X', 'Y', 'Z']) - p1 = Panel(0., **axes) - p1.iloc[0, 0, :] = [1, 2, 3] - expected = Panel(arr, **axes) - tm.assert_panel_equal(p1, expected) - - # multi-indexes - axes['items'] = MultiIndex.from_tuples( - [('A', 'a'), ('B', 'b')]) - p2 = Panel(0., **axes) - p2.iloc[0, 0, :] = [1, 2, 3] - expected = Panel(arr, **axes) - tm.assert_panel_equal(p2, expected) - - axes['major_axis'] = MultiIndex.from_tuples( - [('A', 1), ('A', 2)]) - p3 = Panel(0., **axes) - p3.iloc[0, 0, :] = [1, 2, 3] - expected = Panel(arr, **axes) - tm.assert_panel_equal(p3, expected) - - axes['minor_axis'] = MultiIndex.from_product( - [['X'], range(3)]) - p4 = Panel(0., **axes) - p4.iloc[0, 0, :] = [1, 2, 3] - expected = Panel(arr, **axes) - tm.assert_panel_equal(p4, expected) - - arr = np.array( - [[[1, 0, 0], [2, 0, 0]], [[0, 0, 0], [0, 0, 0]]], - dtype=np.float64) - p5 = Panel(0., **axes) - p5.iloc[0, :, 0] = [1, 2] - expected = Panel(arr, **axes) - tm.assert_panel_equal(p5, expected) diff --git a/pandas/tests/indexing/test_panel.py b/pandas/tests/indexing/test_panel.py index 8530adec011be..8033d19f330b3 100644 --- a/pandas/tests/indexing/test_panel.py +++ b/pandas/tests/indexing/test_panel.py @@ -3,7 +3,7 @@ import numpy as np import pytest -from pandas import DataFrame, Panel, date_range +from pandas import Panel, date_range from pandas.util import testing as tm @@ -31,30 +31,6 @@ def test_iloc_getitem_panel(self): expected = p.loc['B', 'b', 'two'] assert result == expected - # slice - result = p.iloc[1:3] - expected = p.loc[['B', 'C']] - tm.assert_panel_equal(result, expected) - - result = p.iloc[:, 0:2] - expected = p.loc[:, ['a', 'b']] - tm.assert_panel_equal(result, expected) - - # list of integers - result = p.iloc[[0, 2]] - expected = p.loc[['A', 'C']] - tm.assert_panel_equal(result, expected) - - # neg indices - result = p.iloc[[-1, 1], [-1, 1]] - expected = p.loc[['D', 'B'], ['c', 'b']] - tm.assert_panel_equal(result, expected) - - # dups indices - result = p.iloc[[-1, -1, 1], [-1, 1]] - expected = p.loc[['D', 'D', 'B'], ['c', 'b']] - tm.assert_panel_equal(result, expected) - # combined result = p.iloc[0, [True, True], [0, 1]] expected = p.loc['A', ['a', 'b'], ['one', 'two']] @@ -110,18 +86,6 @@ def test_iloc_panel_issue(self): def test_panel_getitem(self): with catch_warnings(record=True): - # GH4016, date selection returns a frame when a partial string - # selection - ind = date_range(start="2000", freq="D", periods=1000) - df = DataFrame( - np.random.randn( - len(ind), 5), index=ind, columns=list('ABCDE')) - panel = Panel({'frame_' + c: df for c in list('ABC')}) - - test2 = panel.loc[:, "2002":"2002-12-31"] - test1 = panel.loc[:, "2002"] - tm.assert_panel_equal(test1, test2) - # with an object-like # GH 9140 class TestObject(object): @@ -138,55 +102,3 @@ def __str__(self): expected = p.iloc[0] result = p[obj] tm.assert_frame_equal(result, expected) - - def test_panel_setitem(self): - - with catch_warnings(record=True): - # GH 7763 - # loc and setitem have setting differences - np.random.seed(0) - index = range(3) - columns = list('abc') - - panel = Panel({'A': DataFrame(np.random.randn(3, 3), - index=index, columns=columns), - 'B': DataFrame(np.random.randn(3, 3), - index=index, columns=columns), - 'C': DataFrame(np.random.randn(3, 3), - index=index, columns=columns)}) - - replace = DataFrame(np.eye(3, 3), index=range(3), columns=columns) - expected = Panel({'A': replace, 'B': replace, 'C': replace}) - - p = panel.copy() - for idx in list('ABC'): - p[idx] = replace - tm.assert_panel_equal(p, expected) - - p = panel.copy() - for idx in list('ABC'): - p.loc[idx, :, :] = replace - tm.assert_panel_equal(p, expected) - - def test_panel_assignment(self): - - with catch_warnings(record=True): - # GH3777 - wp = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], - major_axis=date_range('1/1/2000', periods=5), - minor_axis=['A', 'B', 'C', 'D']) - wp2 = Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], - major_axis=date_range('1/1/2000', periods=5), - minor_axis=['A', 'B', 'C', 'D']) - - # TODO: unused? - # expected = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] - - with pytest.raises(NotImplementedError): - wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = wp2.loc[ - ['Item1', 'Item2'], :, ['A', 'B']] - - # to_assign = wp2.loc[['Item1', 'Item2'], :, ['A', 'B']] - # wp.loc[['Item1', 'Item2'], :, ['A', 'B']] = to_assign - # result = wp.loc[['Item1', 'Item2'], :, ['A', 'B']] - # tm.assert_panel_equal(result,expected) diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 5b6a5ab9ecf7b..e8ce5bc4c36ef 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -10,13 +10,12 @@ import pytest import pandas as pd -from pandas import DataFrame, Index, Panel, Series, date_range +from pandas import DataFrame, Index, Series, date_range from pandas.util import testing as tm class TestPartialSetting(object): - @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") @pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning") def test_partial_setting(self): @@ -116,35 +115,6 @@ def test_partial_setting(self): df.ix[:, 'C'] = df.ix[:, 'A'] tm.assert_frame_equal(df, expected) - with catch_warnings(record=True): - # ## panel ## - p_orig = Panel(np.arange(16).reshape(2, 4, 2), - items=['Item1', 'Item2'], - major_axis=pd.date_range('2001/1/12', periods=4), - minor_axis=['A', 'B'], dtype='float64') - - # panel setting via item - p_orig = Panel(np.arange(16).reshape(2, 4, 2), - items=['Item1', 'Item2'], - major_axis=pd.date_range('2001/1/12', periods=4), - minor_axis=['A', 'B'], dtype='float64') - expected = p_orig.copy() - expected['Item3'] = expected['Item1'] - p = p_orig.copy() - p.loc['Item3'] = p['Item1'] - tm.assert_panel_equal(p, expected) - - # panel with aligned series - expected = p_orig.copy() - expected = expected.transpose(2, 1, 0) - expected['C'] = DataFrame({'Item1': [30, 30, 30, 30], - 'Item2': [32, 32, 32, 32]}, - index=p_orig.major_axis) - expected = expected.transpose(2, 1, 0) - p = p_orig.copy() - p.loc[:, :, 'C'] = Series([30, 32], index=p_orig.items) - tm.assert_panel_equal(p, expected) - # GH 8473 dates = date_range('1/1/2000', periods=8) df_orig = DataFrame(np.random.randn(8, 4), index=dates, diff --git a/pandas/tests/io/generate_legacy_storage_files.py b/pandas/tests/io/generate_legacy_storage_files.py index 6774eac6d6c1a..6c6e28cb1c090 100755 --- a/pandas/tests/io/generate_legacy_storage_files.py +++ b/pandas/tests/io/generate_legacy_storage_files.py @@ -41,7 +41,6 @@ import os import platform as pl import sys -from warnings import catch_warnings, filterwarnings import numpy as np @@ -49,7 +48,7 @@ import pandas from pandas import ( - Categorical, DataFrame, Index, MultiIndex, NaT, Panel, Period, Series, + Categorical, DataFrame, Index, MultiIndex, NaT, Period, Series, SparseDataFrame, SparseSeries, Timestamp, bdate_range, date_range, period_range, timedelta_range, to_msgpack) @@ -187,18 +186,6 @@ def create_data(): u'C': Timestamp('20130603', tz='UTC')}, index=range(5)) ) - with catch_warnings(record=True): - filterwarnings("ignore", "\\nPanel", FutureWarning) - mixed_dup_panel = Panel({u'ItemA': frame[u'float'], - u'ItemB': frame[u'int']}) - mixed_dup_panel.items = [u'ItemA', u'ItemA'] - panel = dict(float=Panel({u'ItemA': frame[u'float'], - u'ItemB': frame[u'float'] + 1}), - dup=Panel( - np.arange(30).reshape(3, 5, 2).astype(np.float64), - items=[u'A', u'B', u'A']), - mixed_dup=mixed_dup_panel) - cat = dict(int8=Categorical(list('abcdefg')), int16=Categorical(np.arange(1000)), int32=Categorical(np.arange(10000))) @@ -241,7 +228,6 @@ def create_data(): return dict(series=series, frame=frame, - panel=panel, index=index, scalars=scalars, mi=mi, diff --git a/pandas/tests/io/test_packers.py b/pandas/tests/io/test_packers.py index 9eb6d327be025..216a4e7b1d7b8 100644 --- a/pandas/tests/io/test_packers.py +++ b/pandas/tests/io/test_packers.py @@ -13,9 +13,8 @@ import pandas from pandas import ( - Categorical, DataFrame, Index, Interval, MultiIndex, NaT, Panel, Period, - Series, Timestamp, bdate_range, compat, date_range, period_range) -from pandas.tests.test_panel import assert_panel_equal + Categorical, DataFrame, Index, Interval, MultiIndex, NaT, Period, Series, + Timestamp, bdate_range, compat, date_range, period_range) import pandas.util.testing as tm from pandas.util.testing import ( assert_categorical_equal, assert_frame_equal, assert_index_equal, @@ -62,8 +61,6 @@ def check_arbitrary(a, b): assert(len(a) == len(b)) for a_, b_ in zip(a, b): check_arbitrary(a_, b_) - elif isinstance(a, Panel): - assert_panel_equal(a, b) elif isinstance(a, DataFrame): assert_frame_equal(a, b) elif isinstance(a, Series): @@ -87,7 +84,6 @@ def check_arbitrary(a, b): assert(a == b) -@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestPackers(object): def setup_method(self, method): @@ -102,7 +98,6 @@ def encode_decode(self, x, compress=None, **kwargs): return read_msgpack(p, **kwargs) -@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestAPI(TestPackers): def test_string_io(self): @@ -467,7 +462,6 @@ def test_basic(self): assert_categorical_equal(i, i_rec) -@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestNDFrame(TestPackers): def setup_method(self, method): @@ -490,23 +484,12 @@ def setup_method(self, method): 'int': DataFrame(dict(A=data['B'], B=Series(data['B']) + 1)), 'mixed': DataFrame(data)} - self.panel = { - 'float': Panel(dict(ItemA=self.frame['float'], - ItemB=self.frame['float'] + 1))} - def test_basic_frame(self): for s, i in self.frame.items(): i_rec = self.encode_decode(i) assert_frame_equal(i, i_rec) - def test_basic_panel(self): - - with catch_warnings(record=True): - for s, i in self.panel.items(): - i_rec = self.encode_decode(i) - assert_panel_equal(i, i_rec) - def test_multi(self): i_rec = self.encode_decode(self.frame) @@ -854,7 +837,6 @@ def legacy_packer(request, datapath): return datapath(request.param) -@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestMsgpack(object): """ How to add msgpack tests: @@ -876,6 +858,10 @@ class TestMsgpack(object): def check_min_structure(self, data, version): for typ, v in self.minimum_structure.items(): + if typ == "panel": + # FIXME: kludge; get this key out of the legacy file + continue + assert typ in data, '"{0}" not found in unpacked data'.format(typ) for kind in v: msg = '"{0}" not found in data["{1}"]'.format(kind, typ) @@ -887,6 +873,11 @@ def compare(self, current_data, all_data, vf, version): data = read_msgpack(vf, encoding='latin-1') else: data = read_msgpack(vf) + + if "panel" in data: + # FIXME: kludge; get the key out of the stored file + del data["panel"] + self.check_min_structure(data, version) for typ, dv in data.items(): assert typ in all_data, ('unpacked data contains ' diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index 7f3fe1aa401ea..b4befadaddc42 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -75,6 +75,10 @@ def compare(data, vf, version): m = globals() for typ, dv in data.items(): + if typ == "panel": + # FIXME: kludge; get this key out of the legacy file + continue + for dt, result in dv.items(): try: expected = data[typ][dt] diff --git a/pandas/tests/test_expressions.py b/pandas/tests/test_expressions.py index f5aa0b0b3c9c8..7a2680135ea80 100644 --- a/pandas/tests/test_expressions.py +++ b/pandas/tests/test_expressions.py @@ -3,19 +3,17 @@ import operator import re -from warnings import catch_warnings, simplefilter import numpy as np from numpy.random import randn import pytest from pandas import _np_version_under1p13, compat -from pandas.core.api import DataFrame, Panel +from pandas.core.api import DataFrame from pandas.core.computation import expressions as expr import pandas.util.testing as tm from pandas.util.testing import ( - assert_almost_equal, assert_frame_equal, assert_panel_equal, - assert_series_equal) + assert_almost_equal, assert_frame_equal, assert_series_equal) from pandas.io.formats.printing import pprint_thing @@ -39,23 +37,6 @@ _integer2 = DataFrame(np.random.randint(1, 100, size=(101, 4)), columns=list('ABCD'), dtype='int64') -with catch_warnings(record=True): - simplefilter("ignore", FutureWarning) - _frame_panel = Panel(dict(ItemA=_frame.copy(), - ItemB=(_frame.copy() + 3), - ItemC=_frame.copy(), - ItemD=_frame.copy())) - _frame2_panel = Panel(dict(ItemA=_frame2.copy(), - ItemB=(_frame2.copy() + 3), - ItemC=_frame2.copy(), - ItemD=_frame2.copy())) - _integer_panel = Panel(dict(ItemA=_integer, - ItemB=(_integer + 34).astype('int64'))) - _integer2_panel = Panel(dict(ItemA=_integer2, - ItemB=(_integer2 + 34).astype('int64'))) - _mixed_panel = Panel(dict(ItemA=_mixed, ItemB=(_mixed + 3))) - _mixed2_panel = Panel(dict(ItemA=_mixed2, ItemB=(_mixed2 + 3))) - @pytest.mark.skipif(not expr._USE_NUMEXPR, reason='not using numexpr') class TestExpressions(object): @@ -173,42 +154,18 @@ def run_series(self, ser, other, binary_comp=None, **kwargs): # self.run_binary(ser, binary_comp, assert_frame_equal, # test_flex=True, **kwargs) - def run_panel(self, panel, other, binary_comp=None, run_binary=True, - assert_func=assert_panel_equal, **kwargs): - self.run_arithmetic(panel, other, assert_func, test_flex=False, - **kwargs) - self.run_arithmetic(panel, other, assert_func, test_flex=True, - **kwargs) - if run_binary: - if binary_comp is None: - binary_comp = other + 1 - self.run_binary(panel, binary_comp, assert_func, - test_flex=False, **kwargs) - self.run_binary(panel, binary_comp, assert_func, - test_flex=True, **kwargs) - def test_integer_arithmetic_frame(self): self.run_frame(self.integer, self.integer) def test_integer_arithmetic_series(self): self.run_series(self.integer.iloc[:, 0], self.integer.iloc[:, 0]) - @pytest.mark.slow - @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") - def test_integer_panel(self): - self.run_panel(_integer2_panel, np.random.randint(1, 100)) - def test_float_arithemtic_frame(self): self.run_frame(self.frame2, self.frame2) def test_float_arithmetic_series(self): self.run_series(self.frame2.iloc[:, 0], self.frame2.iloc[:, 0]) - @pytest.mark.slow - @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") - def test_float_panel(self): - self.run_panel(_frame2_panel, np.random.randn() + 0.1, binary_comp=0.8) - def test_mixed_arithmetic_frame(self): # TODO: FIGURE OUT HOW TO GET IT TO WORK... # can't do arithmetic because comparison methods try to do *entire* @@ -219,12 +176,6 @@ def test_mixed_arithmetic_series(self): for col in self.mixed2.columns: self.run_series(self.mixed2[col], self.mixed2[col], binary_comp=4) - @pytest.mark.slow - @pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") - def test_mixed_panel(self): - self.run_panel(_mixed2_panel, np.random.randint(1, 100), - binary_comp=-2) - def test_float_arithemtic(self): self.run_arithmetic(self.frame, self.frame, assert_frame_equal) self.run_arithmetic(self.frame.iloc[:, 0], self.frame.iloc[:, 0], diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index a7bbbbb5033ac..4ea7e9b8ec9a4 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -15,7 +15,7 @@ from pandas.core.dtypes.common import is_float_dtype, is_integer_dtype import pandas as pd -from pandas import DataFrame, Panel, Series, Timestamp, isna +from pandas import DataFrame, Series, Timestamp, isna from pandas.core.index import Index, MultiIndex import pandas.util.testing as tm @@ -818,18 +818,6 @@ def test_swaplevel(self): exp = self.frame.swaplevel('first', 'second').T tm.assert_frame_equal(swapped, exp) - def test_swaplevel_panel(self): - with catch_warnings(record=True): - simplefilter("ignore", FutureWarning) - panel = Panel({'ItemA': self.frame, 'ItemB': self.frame * 2}) - expected = panel.copy() - expected.major_axis = expected.major_axis.swaplevel(0, 1) - - for result in (panel.swaplevel(axis='major'), - panel.swaplevel(0, axis='major'), - panel.swaplevel(0, 1, axis='major')): - tm.assert_panel_equal(result, expected) - def test_reorder_levels(self): result = self.ymd.reorder_levels(['month', 'day', 'year']) expected = self.ymd.swaplevel(0, 1).swaplevel(1, 2) diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index bfcafda1dc783..b418091de8d7f 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -13,10 +13,9 @@ from pandas.core.panel import Panel import pandas.util.testing as tm from pandas.util.testing import ( - assert_almost_equal, assert_frame_equal, assert_panel_equal, - assert_series_equal, makeCustomDataframe as mkdf, makeMixedDataFrame) + assert_almost_equal, assert_frame_equal, assert_series_equal, + makeCustomDataframe as mkdf, makeMixedDataFrame) -from pandas.io.formats.printing import pprint_thing from pandas.tseries.offsets import MonthEnd @@ -295,25 +294,6 @@ def test_constructor_error_msgs(self): Panel(np.random.randn(3, 4, 5), lrange(5), lrange(5), lrange(4)) - def test_convert_objects(self): - # GH 4937 - p = Panel(dict(A=dict(a=['1', '1.0']))) - expected = Panel(dict(A=dict(a=[1, 1.0]))) - result = p._convert(numeric=True, coerce=True) - assert_panel_equal(result, expected) - - def test_astype(self): - # GH7271 - data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) - panel = Panel(data, ['a', 'b'], ['c', 'd'], ['e', 'f']) - - str_data = np.array([[['1', '2'], ['3', '4']], - [['5', '6'], ['7', '8']]]) - expected = Panel(str_data, ['a', 'b'], ['c', 'd'], ['e', 'f']) - assert_panel_equal(panel.astype(str), expected) - - pytest.raises(NotImplementedError, panel.astype, {0: str}) - def test_apply_slabs(self): # with multi-indexes # GH7469 @@ -350,54 +330,6 @@ def test_apply_no_or_zero_ndim(self): assert_series_equal(result_float, expected_float) assert_series_equal(result_float64, expected_float64) - def test_reindex_axis_style(self): - panel = Panel(np.random.rand(5, 5, 5)) - expected0 = Panel(panel.values).iloc[[0, 1]] - expected1 = Panel(panel.values).iloc[:, [0, 1]] - expected2 = Panel(panel.values).iloc[:, :, [0, 1]] - - result = panel.reindex([0, 1], axis=0) - assert_panel_equal(result, expected0) - - result = panel.reindex([0, 1], axis=1) - assert_panel_equal(result, expected1) - - result = panel.reindex([0, 1], axis=2) - assert_panel_equal(result, expected2) - - result = panel.reindex([0, 1], axis=2) - assert_panel_equal(result, expected2) - - def test_reindex_multi(self): - - # multi-axis indexing consistency - # GH 5900 - df = DataFrame(np.random.randn(4, 3)) - p = Panel({'Item1': df}) - expected = Panel({'Item1': df}) - expected['Item2'] = np.nan - - items = ['Item1', 'Item2'] - major_axis = np.arange(4) - minor_axis = np.arange(3) - - results = [] - results.append(p.reindex(items=items, major_axis=major_axis, - copy=True)) - results.append(p.reindex(items=items, major_axis=major_axis, - copy=False)) - results.append(p.reindex(items=items, minor_axis=minor_axis, - copy=True)) - results.append(p.reindex(items=items, minor_axis=minor_axis, - copy=False)) - results.append(p.reindex(items=items, major_axis=major_axis, - minor_axis=minor_axis, copy=True)) - results.append(p.reindex(items=items, major_axis=major_axis, - minor_axis=minor_axis, copy=False)) - - for i, r in enumerate(results): - assert_panel_equal(expected, r) - def test_fillna(self): # limit not implemented when only value is specified p = Panel(np.random.randn(3, 4, 5)) @@ -405,25 +337,6 @@ def test_fillna(self): pytest.raises(NotImplementedError, lambda: p.fillna(999, limit=1)) - # Test in place fillNA - # Expected result - expected = Panel([[[0, 1], [2, 1]], [[10, 11], [12, 11]]], - items=['a', 'b'], minor_axis=['x', 'y'], - dtype=np.float64) - # method='ffill' - p1 = Panel([[[0, 1], [2, np.nan]], [[10, 11], [12, np.nan]]], - items=['a', 'b'], minor_axis=['x', 'y'], - dtype=np.float64) - p1.fillna(method='ffill', inplace=True) - assert_panel_equal(p1, expected) - - # method='bfill' - p2 = Panel([[[0, np.nan], [2, 1]], [[10, np.nan], [12, 11]]], - items=['a', 'b'], minor_axis=['x', 'y'], - dtype=np.float64) - p2.fillna(method='bfill', inplace=True) - assert_panel_equal(p2, expected) - def test_to_frame_multi_major(self): idx = MultiIndex.from_tuples( [(1, 'one'), (1, 'two'), (2, 'one'), (2, 'two')]) @@ -542,11 +455,6 @@ def test_panel_dups(self): result = panel.loc['E'] assert_frame_equal(result, expected) - expected = no_dup_panel.loc[['A', 'B']] - expected.items = ['A', 'A'] - result = panel.loc['A'] - assert_panel_equal(result, expected) - # major data = np.random.randn(5, 5, 5) no_dup_panel = Panel(data, major_axis=list("ABCDE")) @@ -560,11 +468,6 @@ def test_panel_dups(self): result = panel.loc[:, 'E'] assert_frame_equal(result, expected) - expected = no_dup_panel.loc[:, ['A', 'B']] - expected.major_axis = ['A', 'A'] - result = panel.loc[:, 'A'] - assert_panel_equal(result, expected) - # minor data = np.random.randn(5, 100, 5) no_dup_panel = Panel(data, minor_axis=list("ABCDE")) @@ -578,11 +481,6 @@ def test_panel_dups(self): result = panel.loc[:, :, 'E'] assert_frame_equal(result, expected) - expected = no_dup_panel.loc[:, :, ['A', 'B']] - expected.minor_axis = ['A', 'A'] - result = panel.loc[:, :, 'A'] - assert_panel_equal(result, expected) - def test_filter(self): pass @@ -595,93 +493,14 @@ def test_shift(self): shifted = mixed_panel.shift(1) assert_series_equal(mixed_panel.dtypes, shifted.dtypes) - def test_pct_change(self): - df1 = DataFrame({'c1': [1, 2, 5], 'c2': [3, 4, 6]}) - df2 = df1 + 1 - df3 = DataFrame({'c1': [3, 4, 7], 'c2': [5, 6, 8]}) - wp = Panel({'i1': df1, 'i2': df2, 'i3': df3}) - # major, 1 - result = wp.pct_change() # axis='major' - expected = Panel({'i1': df1.pct_change(), - 'i2': df2.pct_change(), - 'i3': df3.pct_change()}) - assert_panel_equal(result, expected) - result = wp.pct_change(axis=1) - assert_panel_equal(result, expected) - # major, 2 - result = wp.pct_change(periods=2) - expected = Panel({'i1': df1.pct_change(2), - 'i2': df2.pct_change(2), - 'i3': df3.pct_change(2)}) - assert_panel_equal(result, expected) - # minor, 1 - result = wp.pct_change(axis='minor') - expected = Panel({'i1': df1.pct_change(axis=1), - 'i2': df2.pct_change(axis=1), - 'i3': df3.pct_change(axis=1)}) - assert_panel_equal(result, expected) - result = wp.pct_change(axis=2) - assert_panel_equal(result, expected) - # minor, 2 - result = wp.pct_change(periods=2, axis='minor') - expected = Panel({'i1': df1.pct_change(periods=2, axis=1), - 'i2': df2.pct_change(periods=2, axis=1), - 'i3': df3.pct_change(periods=2, axis=1)}) - assert_panel_equal(result, expected) - # items, 1 - result = wp.pct_change(axis='items') - expected = Panel( - {'i1': DataFrame({'c1': [np.nan, np.nan, np.nan], - 'c2': [np.nan, np.nan, np.nan]}), - 'i2': DataFrame({'c1': [1, 0.5, .2], - 'c2': [1. / 3, 0.25, 1. / 6]}), - 'i3': DataFrame({'c1': [.5, 1. / 3, 1. / 6], - 'c2': [.25, .2, 1. / 7]})}) - assert_panel_equal(result, expected) - result = wp.pct_change(axis=0) - assert_panel_equal(result, expected) - # items, 2 - result = wp.pct_change(periods=2, axis='items') - expected = Panel( - {'i1': DataFrame({'c1': [np.nan, np.nan, np.nan], - 'c2': [np.nan, np.nan, np.nan]}), - 'i2': DataFrame({'c1': [np.nan, np.nan, np.nan], - 'c2': [np.nan, np.nan, np.nan]}), - 'i3': DataFrame({'c1': [2, 1, .4], - 'c2': [2. / 3, .5, 1. / 3]})}) - assert_panel_equal(result, expected) - - def test_round(self): - values = [[[-3.2, 2.2], [0, -4.8213], [3.123, 123.12], - [-1566.213, 88.88], [-12, 94.5]], - [[-5.82, 3.5], [6.21, -73.272], [-9.087, 23.12], - [272.212, -99.99], [23, -76.5]]] - evalues = [[[float(np.around(i)) for i in j] for j in k] - for k in values] - p = Panel(values, items=['Item1', 'Item2'], - major_axis=date_range('1/1/2000', periods=5), - minor_axis=['A', 'B']) - expected = Panel(evalues, items=['Item1', 'Item2'], - major_axis=date_range('1/1/2000', periods=5), - minor_axis=['A', 'B']) - result = p.round() - assert_panel_equal(expected, result) - def test_numpy_round(self): values = [[[-3.2, 2.2], [0, -4.8213], [3.123, 123.12], [-1566.213, 88.88], [-12, 94.5]], [[-5.82, 3.5], [6.21, -73.272], [-9.087, 23.12], [272.212, -99.99], [23, -76.5]]] - evalues = [[[float(np.around(i)) for i in j] for j in k] - for k in values] p = Panel(values, items=['Item1', 'Item2'], major_axis=date_range('1/1/2000', periods=5), minor_axis=['A', 'B']) - expected = Panel(evalues, items=['Item1', 'Item2'], - major_axis=date_range('1/1/2000', periods=5), - minor_axis=['A', 'B']) - result = np.round(p) - assert_panel_equal(expected, result) msg = "the 'out' parameter is not supported" with pytest.raises(ValueError, match=msg): @@ -699,7 +518,6 @@ def test_multiindex_get(self): minor_axis=np.arange(5)) f1 = wp['a'] f2 = wp.loc['a'] - assert_panel_equal(f1, f2) assert (f1.items == [1, 2]).all() assert (f2.items == [1, 2]).all() @@ -711,178 +529,6 @@ def test_repr_empty(self): empty = Panel() repr(empty) - @pytest.mark.filterwarnings("ignore:'.reindex:FutureWarning") - def test_dropna(self): - p = Panel(np.random.randn(4, 5, 6), major_axis=list('abcde')) - p.loc[:, ['b', 'd'], 0] = np.nan - - result = p.dropna(axis=1) - exp = p.loc[:, ['a', 'c', 'e'], :] - assert_panel_equal(result, exp) - inp = p.copy() - inp.dropna(axis=1, inplace=True) - assert_panel_equal(inp, exp) - - result = p.dropna(axis=1, how='all') - assert_panel_equal(result, p) - - p.loc[:, ['b', 'd'], :] = np.nan - result = p.dropna(axis=1, how='all') - exp = p.loc[:, ['a', 'c', 'e'], :] - assert_panel_equal(result, exp) - - p = Panel(np.random.randn(4, 5, 6), items=list('abcd')) - p.loc[['b'], :, 0] = np.nan - - result = p.dropna() - exp = p.loc[['a', 'c', 'd']] - assert_panel_equal(result, exp) - - result = p.dropna(how='all') - assert_panel_equal(result, p) - - p.loc['b'] = np.nan - result = p.dropna(how='all') - exp = p.loc[['a', 'c', 'd']] - assert_panel_equal(result, exp) - - def test_drop(self): - df = DataFrame({"A": [1, 2], "B": [3, 4]}) - panel = Panel({"One": df, "Two": df}) - - def check_drop(drop_val, axis_number, aliases, expected): - try: - actual = panel.drop(drop_val, axis=axis_number) - assert_panel_equal(actual, expected) - for alias in aliases: - actual = panel.drop(drop_val, axis=alias) - assert_panel_equal(actual, expected) - except AssertionError: - pprint_thing("Failed with axis_number %d and aliases: %s" % - (axis_number, aliases)) - raise - # Items - expected = Panel({"One": df}) - check_drop('Two', 0, ['items'], expected) - - pytest.raises(KeyError, panel.drop, 'Three') - - # errors = 'ignore' - dropped = panel.drop('Three', errors='ignore') - assert_panel_equal(dropped, panel) - dropped = panel.drop(['Two', 'Three'], errors='ignore') - expected = Panel({"One": df}) - assert_panel_equal(dropped, expected) - - # Major - exp_df = DataFrame({"A": [2], "B": [4]}, index=[1]) - expected = Panel({"One": exp_df, "Two": exp_df}) - check_drop(0, 1, ['major_axis', 'major'], expected) - - exp_df = DataFrame({"A": [1], "B": [3]}, index=[0]) - expected = Panel({"One": exp_df, "Two": exp_df}) - check_drop([1], 1, ['major_axis', 'major'], expected) - - # Minor - exp_df = df[['B']] - expected = Panel({"One": exp_df, "Two": exp_df}) - check_drop(["A"], 2, ['minor_axis', 'minor'], expected) - - exp_df = df[['A']] - expected = Panel({"One": exp_df, "Two": exp_df}) - check_drop("B", 2, ['minor_axis', 'minor'], expected) - - def test_update(self): - pan = Panel([[[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]], - [[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]]) - - other = Panel( - [[[3.6, 2., np.nan], [np.nan, np.nan, 7]]], items=[1]) - - pan.update(other) - - expected = Panel([[[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], [1.5, np.nan, 3.]], - [[3.6, 2., 3], [1.5, np.nan, 7], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]]) - - assert_panel_equal(pan, expected) - - def test_update_from_dict(self): - pan = Panel({'one': DataFrame([[1.5, np.nan, 3], - [1.5, np.nan, 3], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]), - 'two': DataFrame([[1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]])}) - - other = {'two': DataFrame( - [[3.6, 2., np.nan], [np.nan, np.nan, 7]])} - - pan.update(other) - - expected = Panel( - {'one': DataFrame([[1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]), - 'two': DataFrame([[3.6, 2., 3], - [1.5, np.nan, 7], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]) - } - ) - - assert_panel_equal(pan, expected) - - def test_update_nooverwrite(self): - pan = Panel([[[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]], - [[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]]) - - other = Panel( - [[[3.6, 2., np.nan], [np.nan, np.nan, 7]]], items=[1]) - - pan.update(other, overwrite=False) - - expected = Panel([[[1.5, np.nan, 3], [1.5, np.nan, 3], - [1.5, np.nan, 3.], [1.5, np.nan, 3.]], - [[1.5, 2., 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]]) - - assert_panel_equal(pan, expected) - - def test_update_filtered(self): - pan = Panel([[[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]], - [[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], - [1.5, np.nan, 3.]]]) - - other = Panel( - [[[3.6, 2., np.nan], [np.nan, np.nan, 7]]], items=[1]) - - pan.update(other, filter_func=lambda x: x > 2) - - expected = Panel([[[1.5, np.nan, 3.], [1.5, np.nan, 3.], - [1.5, np.nan, 3.], [1.5, np.nan, 3.]], - [[1.5, np.nan, 3], [1.5, np.nan, 7], - [1.5, np.nan, 3.], [1.5, np.nan, 3.]]]) - - assert_panel_equal(pan, expected) - @pytest.mark.parametrize('bad_kwarg, exception, msg', [ # errors must be 'ignore' or 'raise' ({'errors': 'something'}, ValueError, 'The parameter errors must.*'), diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 387f402348513..a5ae1f6a4d960 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1503,69 +1503,6 @@ def assert_frame_equal(left, right, check_dtype=True, obj='DataFrame.iloc[:, {idx}]'.format(idx=i)) -def assert_panel_equal(left, right, - check_dtype=True, - check_panel_type=False, - check_less_precise=False, - check_names=False, - by_blocks=False, - obj='Panel'): - """Check that left and right Panels are equal. - - Parameters - ---------- - left : Panel (or nd) - right : Panel (or nd) - check_dtype : bool, default True - Whether to check the Panel dtype is identical. - check_panel_type : bool, default False - Whether to check the Panel class is identical. - check_less_precise : bool or int, default False - Specify comparison precision. Only used when check_exact is False. - 5 digits (False) or 3 digits (True) after decimal points are compared. - If int, then specify the digits to compare - check_names : bool, default True - Whether to check the Index names attribute. - by_blocks : bool, default False - Specify how to compare internal data. If False, compare by columns. - If True, compare by blocks. - obj : str, default 'Panel' - Specify the object name being compared, internally used to show - the appropriate assertion message. - """ - - if check_panel_type: - assert_class_equal(left, right, obj=obj) - - for axis in left._AXIS_ORDERS: - left_ind = getattr(left, axis) - right_ind = getattr(right, axis) - assert_index_equal(left_ind, right_ind, check_names=check_names) - - if by_blocks: - rblocks = right._to_dict_of_blocks() - lblocks = left._to_dict_of_blocks() - for dtype in list(set(list(lblocks.keys()) + list(rblocks.keys()))): - assert dtype in lblocks - assert dtype in rblocks - array_equivalent(lblocks[dtype].values, rblocks[dtype].values) - else: - - # can potentially be slow - for i, item in enumerate(left._get_axis(0)): - msg = "non-matching item (right) '{item}'".format(item=item) - assert item in right, msg - litem = left.iloc[i] - ritem = right.iloc[i] - assert_frame_equal(litem, ritem, - check_less_precise=check_less_precise, - check_names=check_names) - - for i, item in enumerate(right._get_axis(0)): - msg = "non-matching item (left) '{item}'".format(item=item) - assert item in left, msg - - def assert_equal(left, right, **kwargs): """ Wrapper for tm.assert_*_equal to dispatch to the appropriate test function. From 36b0bb6071e82c6d5d9905a70c840286fa9503ff Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 9 Feb 2019 09:21:55 -0800 Subject: [PATCH 2/2] restore warning filter --- pandas/tests/io/test_packers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/io/test_packers.py b/pandas/tests/io/test_packers.py index 216a4e7b1d7b8..375557c43a3ae 100644 --- a/pandas/tests/io/test_packers.py +++ b/pandas/tests/io/test_packers.py @@ -84,6 +84,7 @@ def check_arbitrary(a, b): assert(a == b) +@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestPackers(object): def setup_method(self, method): @@ -98,6 +99,7 @@ def encode_decode(self, x, compress=None, **kwargs): return read_msgpack(p, **kwargs) +@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestAPI(TestPackers): def test_string_io(self): @@ -462,6 +464,7 @@ def test_basic(self): assert_categorical_equal(i, i_rec) +@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestNDFrame(TestPackers): def setup_method(self, method): @@ -837,6 +840,7 @@ def legacy_packer(request, datapath): return datapath(request.param) +@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning") class TestMsgpack(object): """ How to add msgpack tests: