示例#1
0
 def setUp(self):
     self.data_dict = {
         'ItemA': panel_data1(),
         'ItemB': panel_data2(),
         'ItemC': panel_data3(),
         'ItemD': panel_data1(),
     }
     self.panel = SparsePanel(self.data_dict)
示例#2
0
 def setUp(self):
     self.data_dict = {
         'ItemA': panel_data1(),
         'ItemB': panel_data2(),
         'ItemC': panel_data3(),
         'ItemD': panel_data1(),
     }
     with tm.assert_produces_warning(FutureWarning):
         self.panel = SparsePanel(self.data_dict)
示例#3
0
 def setUp(self):
     self.data_dict = {
         "ItemA": panel_data1(),
         "ItemB": panel_data2(),
         "ItemC": panel_data3(),
         "ItemD": panel_data1(),
     }
     self.panel = SparsePanel(self.data_dict)
示例#4
0
 def test_constructor(self):
     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'])
示例#5
0
 def setUp(self):
     self.data_dict = {
         'ItemA' : panel_data1(),
         'ItemB' : panel_data2(),
         'ItemC' : panel_data3(),
         'ItemD' : panel_data1(),
     }
     self.panel = SparsePanel(self.data_dict)
示例#6
0
 def setUp(self):
     self.data_dict = {
         'ItemA': panel_data1(),
         'ItemB': panel_data2(),
         'ItemC': panel_data3(),
         'ItemD': panel_data1(),
     }
     with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
         self.panel = SparsePanel(self.data_dict)
示例#7
0
    def _read_sparse_panel(self, group, where=None):
        default_fill_value = getattr(group._v_attrs, 'default_fill_value')
        default_kind = getattr(group._v_attrs, 'default_kind')
        items = self._read_index(group, 'items')

        sdict = {}
        for name in items:
            key = 'sparse_frame_%s' % name
            node = getattr(group, key)
            sdict[name] = self._read_sparse_frame(node)
        return SparsePanel(sdict, items=items, default_kind=default_kind,
                           default_fill_value=default_fill_value)
示例#8
0
    def test_to_frame(self):
        def _compare_with_dense(panel):
            slp = panel.to_frame()
            dlp = panel.to_dense().to_frame()

            self.assert_(np.array_equal(slp.values, dlp.values))
            self.assert_(slp.index.equals(dlp.index))

        _compare_with_dense(self.panel)
        _compare_with_dense(self.panel.reindex(items=['ItemA']))

        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)
示例#9
0
    def test_to_frame(self):

        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):

            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))

            _compare_with_dense(self.panel)
            _compare_with_dense(self.panel.reindex(items=['ItemA']))

            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)
示例#10
0
 def test_from_dict(self):
     with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
         fd = SparsePanel.from_dict(self.data_dict)
     tm.assert_sp_panel_equal(fd, self.panel)
示例#11
0
class TestSparsePanel(tm.TestCase, test_panel.SafeForLongAndSparse,
                      test_panel.SafeForSparse):
    _multiprocess_can_split_ = True

    def setUp(self):
        self.data_dict = {
            'ItemA': panel_data1(),
            'ItemB': panel_data2(),
            'ItemC': panel_data3(),
            'ItemD': panel_data1(),
        }
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            self.panel = SparsePanel(self.data_dict)

    @staticmethod
    def _test_op(panel, op):
        # arithmetic tests
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            result = op(panel, 1)
        tm.assert_sp_frame_equal(result['ItemA'], op(panel['ItemA'], 1))

    def test_constructor(self):
        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):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            SparsePanel()

    # GH 9272
    def test_constructor_empty(self):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            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):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            fd = SparsePanel.from_dict(self.data_dict)
        tm.assert_sp_panel_equal(fd, self.panel)

    def test_pickle(self):
        def _test_roundtrip(panel):
            result = self.round_trip_pickle(panel)
            tm.assertIsInstance(result.items, Index)
            tm.assertIsInstance(result.major_axis, Index)
            tm.assertIsInstance(result.minor_axis, Index)
            tm.assert_sp_panel_equal(panel, result)

        _test_roundtrip(self.panel)

    def test_dense_to_sparse(self):
        wp = Panel.from_dict(self.data_dict)
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            dwp = wp.to_sparse()
        tm.assertIsInstance(dwp['ItemA']['A'], SparseSeries)

    def test_to_dense(self):
        dwp = self.panel.to_dense()
        dwp2 = Panel.from_dict(self.data_dict)
        tm.assert_panel_equal(dwp, dwp2)

    def test_to_frame(self):

        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):

            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.assert_index_equal(slp.index, dlp.index,
                                        check_names=False)

            _compare_with_dense(self.panel)
            _compare_with_dense(self.panel.reindex(items=['ItemA']))

            with tm.assert_produces_warning(FutureWarning,
                                            check_stacklevel=False):
                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

    def test_values(self):
        pass

    def test_setitem(self):
        self.panel['ItemE'] = self.panel['ItemC']
        self.panel['ItemF'] = self.panel['ItemC'].to_dense()

        tm.assert_sp_frame_equal(self.panel['ItemE'], self.panel['ItemC'])
        tm.assert_sp_frame_equal(self.panel['ItemF'], self.panel['ItemC'])

        expected = pd.Index(['ItemA', 'ItemB', 'ItemC',
                             'ItemD', 'ItemE', 'ItemF'])
        tm.assert_index_equal(self.panel.items, expected)

        self.assertRaises(Exception, self.panel.__setitem__, 'item6', 1)

    def test_set_value(self):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            def _check_loc(item, major, minor, val=1.5):
                res = self.panel.set_value(item, major, minor, val)
                self.assertIsNot(res, self.panel)
                self.assertEqual(res.get_value(item, major, minor), val)

            _check_loc('ItemA', self.panel.major_axis[4],
                       self.panel.minor_axis[3])
            _check_loc('ItemF', self.panel.major_axis[4],
                       self.panel.minor_axis[3])
            _check_loc('ItemF', 'foo', self.panel.minor_axis[3])
            _check_loc('ItemE', 'foo', 'bar')

    def test_delitem_pop(self):
        del self.panel['ItemB']
        tm.assert_index_equal(self.panel.items,
                              pd.Index(['ItemA', 'ItemC', 'ItemD']))
        crackle = self.panel['ItemC']
        pop = self.panel.pop('ItemC')
        self.assertIs(pop, crackle)
        tm.assert_almost_equal(self.panel.items, pd.Index(['ItemA', 'ItemD']))

        self.assertRaises(KeyError, self.panel.__delitem__, 'ItemC')

    def test_copy(self):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            cop = self.panel.copy()
        tm.assert_sp_panel_equal(cop, self.panel)

    def test_reindex(self):
        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)
                tm.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):
                with tm.assert_produces_warning(FutureWarning,
                                                check_stacklevel=False):
                    dense = panel.to_dense()
                    sparse_result = op(panel)
                    dense_result = op(dense)
                    tm.assert_panel_equal(sparse_result.to_dense(),
                                          dense_result)

            def _mixed_comp(op):
                with tm.assert_produces_warning(FutureWarning,
                                                check_stacklevel=False):
                    result = op(panel, panel.to_dense())
                    expected = op(panel.to_dense(), panel.to_dense())
                    tm.assert_panel_equal(result, expected)

            op1 = lambda x: x + 2

            _dense_comp(op1)
            op2 = lambda x: x.add(x.reindex(major=x.major_axis[::2]))
            _dense_comp(op2)
            op3 = lambda x: x.subtract(x.mean(0), axis=0)
            _dense_comp(op3)
            op4 = lambda x: x.subtract(x.mean(1), axis=1)
            _dense_comp(op4)
            op5 = lambda x: x.subtract(x.mean(2), axis=2)
            _dense_comp(op5)

            _mixed_comp(Panel.multiply)
            _mixed_comp(Panel.subtract)

            # TODO: this case not yet supported!
            # op6 = lambda x: x.add(x.to_frame())
            # _dense_comp(op6)

        _check_ops(self.panel)

    def test_major_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.major_axis:
                dslice = dense.major_xs(idx)
                sslice = sparse.major_xs(idx)
                tm.assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)

    def test_minor_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.minor_axis:
                dslice = dense.minor_xs(idx)
                sslice = sparse.minor_xs(idx).to_dense()
                tm.assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)
示例#12
0
 def test_from_dict(self):
     fd = SparsePanel.from_dict(self.data_dict)
     assert_sp_panel_equal(fd, self.panel)
示例#13
0
 def test_constructor_empty(self):
     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)
示例#14
0
 def test_from_dict(self):
     with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
         fd = SparsePanel.from_dict(self.data_dict)
     tm.assert_sp_panel_equal(fd, self.panel)
示例#15
0
 def test_deprecation(self):
     with tm.assert_produces_warning(FutureWarning):
         SparsePanel()
示例#16
0
class TestSparsePanel(TestCase, test_panel.SafeForLongAndSparse, test_panel.SafeForSparse):
    @classmethod
    def assert_panel_equal(cls, x, y):
        assert_sp_panel_equal(x, y)

    def setUp(self):
        self.data_dict = {
            "ItemA": panel_data1(),
            "ItemB": panel_data2(),
            "ItemC": panel_data3(),
            "ItemD": panel_data1(),
        }
        self.panel = SparsePanel(self.data_dict)

    @staticmethod
    def _test_op(panel, op):
        # arithmetic tests
        result = op(panel, 1)
        assert_sp_frame_equal(result["ItemA"], op(panel["ItemA"], 1))

    def test_constructor(self):
        self.assertRaises(Exception, SparsePanel, self.data_dict, items=["Item0", "ItemA", "ItemB"])

    def test_from_dict(self):
        fd = SparsePanel.from_dict(self.data_dict)
        assert_sp_panel_equal(fd, self.panel)

    def test_pickle(self):
        def _test_roundtrip(panel):
            pickled = pickle.dumps(panel, protocol=pickle.HIGHEST_PROTOCOL)
            unpickled = pickle.loads(pickled)
            assert_sp_panel_equal(panel, unpickled)

        _test_roundtrip(self.panel)

    def test_dense_to_sparse(self):
        wp = Panel.from_dict(self.data_dict)
        dwp = wp.to_sparse()
        self.assert_(isinstance(dwp["ItemA"]["A"], SparseSeries))

    def test_to_dense(self):
        dwp = self.panel.to_dense()
        dwp2 = Panel.from_dict(self.data_dict)
        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_(np.array_equal(slp.values, dlp.values))
            self.assert_(slp.index.equals(dlp.index))

        _compare_with_dense(self.panel)
        _compare_with_dense(self.panel.reindex(items=["ItemA"]))

        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

    def test_values(self):
        pass

    def test_setitem(self):
        self.panel["ItemE"] = self.panel["ItemC"]
        self.panel["ItemF"] = self.panel["ItemC"].to_dense()

        assert_sp_frame_equal(self.panel["ItemE"], self.panel["ItemC"])
        assert_sp_frame_equal(self.panel["ItemF"], self.panel["ItemC"])
        assert_almost_equal(self.panel.items, ["ItemA", "ItemB", "ItemC", "ItemD", "ItemE", "ItemF"])

        self.assertRaises(Exception, self.panel.__setitem__, "item6", 1)

    def test_set_value(self):
        def _check_loc(item, major, minor, val=1.5):
            res = self.panel.set_value(item, major, minor, val)
            self.assert_(res is not self.panel)
            self.assertEquals(res.get_value(item, major, minor), val)

        _check_loc("ItemA", self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc("ItemF", self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc("ItemF", "foo", self.panel.minor_axis[3])
        _check_loc("ItemE", "foo", "bar")

    def test_delitem_pop(self):
        del self.panel["ItemB"]
        assert_almost_equal(self.panel.items, ["ItemA", "ItemC", "ItemD"])
        crackle = self.panel["ItemC"]
        pop = self.panel.pop("ItemC")
        self.assert_(pop is crackle)
        assert_almost_equal(self.panel.items, ["ItemA", "ItemD"])

        self.assertRaises(KeyError, self.panel.__delitem__, "ItemC")

    def test_copy(self):
        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.assert_("E" not in 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)

            op1 = lambda x: x + 2

            _dense_comp(op1)
            op2 = lambda x: x.add(x.reindex(major=x.major_axis[::2]))
            _dense_comp(op2)
            op3 = lambda x: x.subtract(x.mean(0), axis=0)
            _dense_comp(op3)
            op4 = lambda x: x.subtract(x.mean(1), axis=1)
            _dense_comp(op4)
            op5 = lambda x: x.subtract(x.mean(2), axis=2)
            _dense_comp(op5)

            # TODO: this case not yet supported!
            # op6 = lambda x: x.add(x.to_frame())
            # _dense_comp(op6)

        _check_ops(self.panel)

    def test_major_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.major_axis:
                dslice = dense.major_xs(idx)
                sslice = sparse.major_xs(idx)
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)

    def test_minor_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.minor_axis:
                dslice = dense.minor_xs(idx)
                sslice = sparse.minor_xs(idx).to_dense()
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)
示例#17
0
 def test_from_dict(self):
     fd = SparsePanel.from_dict(self.data_dict)
     assert_sp_panel_equal(fd, self.panel)
示例#18
0
class TestSparsePanel(tm.TestCase, test_panel.SafeForLongAndSparse,
                      test_panel.SafeForSparse):
    _multiprocess_can_split_ = True

    @classmethod
    def assert_panel_equal(cls, x, y):
        tm.assert_sp_panel_equal(x, y)

    def setUp(self):
        self.data_dict = {
            'ItemA': panel_data1(),
            'ItemB': panel_data2(),
            'ItemC': panel_data3(),
            'ItemD': panel_data1(),
        }
        with tm.assert_produces_warning(FutureWarning):
            self.panel = SparsePanel(self.data_dict)

    @staticmethod
    def _test_op(panel, op):
        # arithmetic tests
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            result = op(panel, 1)
        tm.assert_sp_frame_equal(result['ItemA'], op(panel['ItemA'], 1))

    def test_constructor(self):
        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):
        with tm.assert_produces_warning(FutureWarning):
            SparsePanel()

    # GH 9272
    def test_constructor_empty(self):
        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):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            fd = SparsePanel.from_dict(self.data_dict)
        tm.assert_sp_panel_equal(fd, self.panel)

    def test_pickle(self):
        def _test_roundtrip(panel):
            result = self.round_trip_pickle(panel)
            tm.assertIsInstance(result.items, Index)
            tm.assertIsInstance(result.major_axis, Index)
            tm.assertIsInstance(result.minor_axis, Index)
            tm.assert_sp_panel_equal(panel, result)

        _test_roundtrip(self.panel)

    def test_dense_to_sparse(self):
        wp = Panel.from_dict(self.data_dict)
        dwp = wp.to_sparse()
        tm.assertIsInstance(dwp['ItemA']['A'], SparseSeries)

    def test_to_dense(self):
        dwp = self.panel.to_dense()
        dwp2 = Panel.from_dict(self.data_dict)
        assert_panel_equal(dwp, dwp2)

    def test_to_frame(self):

        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):

            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))

            _compare_with_dense(self.panel)
            _compare_with_dense(self.panel.reindex(items=['ItemA']))

            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

    def test_values(self):
        pass

    def test_setitem(self):
        self.panel['ItemE'] = self.panel['ItemC']
        self.panel['ItemF'] = self.panel['ItemC'].to_dense()

        tm.assert_sp_frame_equal(self.panel['ItemE'], self.panel['ItemC'])
        tm.assert_sp_frame_equal(self.panel['ItemF'], self.panel['ItemC'])

        expected = pd.Index(['ItemA', 'ItemB', 'ItemC',
                             'ItemD', 'ItemE', 'ItemF'])
        tm.assert_index_equal(self.panel.items, expected)

        self.assertRaises(Exception, self.panel.__setitem__, 'item6', 1)

    def test_set_value(self):
        def _check_loc(item, major, minor, val=1.5):
            res = self.panel.set_value(item, major, minor, val)
            self.assertIsNot(res, self.panel)
            self.assertEqual(res.get_value(item, major, minor), val)

        _check_loc('ItemA', self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc('ItemF', self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc('ItemF', 'foo', self.panel.minor_axis[3])
        _check_loc('ItemE', 'foo', 'bar')

    def test_delitem_pop(self):
        del self.panel['ItemB']
        tm.assert_index_equal(self.panel.items,
                              pd.Index(['ItemA', 'ItemC', 'ItemD']))
        crackle = self.panel['ItemC']
        pop = self.panel.pop('ItemC')
        self.assertIs(pop, crackle)
        tm.assert_almost_equal(self.panel.items, pd.Index(['ItemA', 'ItemD']))

        self.assertRaises(KeyError, self.panel.__delitem__, 'ItemC')

    def test_copy(self):
        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
            cop = self.panel.copy()
        tm.assert_sp_panel_equal(cop, self.panel)

    def test_reindex(self):
        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):
                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):
                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

            _dense_comp(op1)
            op2 = lambda x: x.add(x.reindex(major=x.major_axis[::2]))
            _dense_comp(op2)
            op3 = lambda x: x.subtract(x.mean(0), axis=0)
            _dense_comp(op3)
            op4 = lambda x: x.subtract(x.mean(1), axis=1)
            _dense_comp(op4)
            op5 = lambda x: x.subtract(x.mean(2), axis=2)
            _dense_comp(op5)

            _mixed_comp(Panel.multiply)
            _mixed_comp(Panel.subtract)

            # TODO: this case not yet supported!
            # op6 = lambda x: x.add(x.to_frame())
            # _dense_comp(op6)

        _check_ops(self.panel)

    def test_major_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.major_axis:
                dslice = dense.major_xs(idx)
                sslice = sparse.major_xs(idx)
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)

    def test_minor_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.minor_axis:
                dslice = dense.minor_xs(idx)
                sslice = sparse.minor_xs(idx).to_dense()
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)
示例#19
0
class TestSparsePanel(TestCase, test_panel.SafeForLongAndSparse,
                      test_panel.SafeForSparse):
    @classmethod
    def assert_panel_equal(cls, x, y):
        assert_sp_panel_equal(x, y)

    def setUp(self):
        self.data_dict = {
            'ItemA': panel_data1(),
            'ItemB': panel_data2(),
            'ItemC': panel_data3(),
            'ItemD': panel_data1(),
        }
        self.panel = SparsePanel(self.data_dict)

    @staticmethod
    def _test_op(panel, op):
        # arithmetic tests
        result = op(panel, 1)
        assert_sp_frame_equal(result['ItemA'], op(panel['ItemA'], 1))

    def test_constructor(self):
        self.assertRaises(Exception,
                          SparsePanel,
                          self.data_dict,
                          items=['Item0', 'ItemA', 'ItemB'])

    def test_from_dict(self):
        fd = SparsePanel.from_dict(self.data_dict)
        assert_sp_panel_equal(fd, self.panel)

    def test_pickle(self):
        def _test_roundtrip(panel):
            pickled = pickle.dumps(panel, protocol=pickle.HIGHEST_PROTOCOL)
            unpickled = pickle.loads(pickled)
            assert_sp_panel_equal(panel, unpickled)

        _test_roundtrip(self.panel)

    def test_dense_to_sparse(self):
        wp = Panel.from_dict(self.data_dict)
        dwp = wp.to_sparse()
        self.assert_(isinstance(dwp['ItemA']['A'], SparseSeries))

    def test_to_dense(self):
        dwp = self.panel.to_dense()
        dwp2 = Panel.from_dict(self.data_dict)
        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_(np.array_equal(slp.values, dlp.values))
            self.assert_(slp.index.equals(dlp.index))

        _compare_with_dense(self.panel)
        _compare_with_dense(self.panel.reindex(items=['ItemA']))

        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

    def test_values(self):
        pass

    def test_setitem(self):
        self.panel['ItemE'] = self.panel['ItemC']
        self.panel['ItemF'] = self.panel['ItemC'].to_dense()

        assert_sp_frame_equal(self.panel['ItemE'], self.panel['ItemC'])
        assert_sp_frame_equal(self.panel['ItemF'], self.panel['ItemC'])
        assert_almost_equal(
            self.panel.items,
            ['ItemA', 'ItemB', 'ItemC', 'ItemD', 'ItemE', 'ItemF'])

        self.assertRaises(Exception, self.panel.__setitem__, 'item6', 1)

    def test_set_value(self):
        def _check_loc(item, major, minor, val=1.5):
            res = self.panel.set_value(item, major, minor, val)
            self.assert_(res is not self.panel)
            self.assertEquals(res.get_value(item, major, minor), val)

        _check_loc('ItemA', self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc('ItemF', self.panel.major_axis[4], self.panel.minor_axis[3])
        _check_loc('ItemF', 'foo', self.panel.minor_axis[3])
        _check_loc('ItemE', 'foo', 'bar')

    def test_delitem_pop(self):
        del self.panel['ItemB']
        assert_almost_equal(self.panel.items, ['ItemA', 'ItemC', 'ItemD'])
        crackle = self.panel['ItemC']
        pop = self.panel.pop('ItemC')
        self.assert_(pop is crackle)
        assert_almost_equal(self.panel.items, ['ItemA', 'ItemD'])

        self.assertRaises(KeyError, self.panel.__delitem__, 'ItemC')

    def test_copy(self):
        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.assert_('E' not in 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)

            op1 = lambda x: x + 2

            _dense_comp(op1)
            op2 = lambda x: x.add(x.reindex(major=x.major_axis[::2]))
            _dense_comp(op2)
            op3 = lambda x: x.subtract(x.mean(0), axis=0)
            _dense_comp(op3)
            op4 = lambda x: x.subtract(x.mean(1), axis=1)
            _dense_comp(op4)
            op5 = lambda x: x.subtract(x.mean(2), axis=2)
            _dense_comp(op5)

            # TODO: this case not yet supported!
            # op6 = lambda x: x.add(x.to_frame())
            # _dense_comp(op6)

        _check_ops(self.panel)

    def test_major_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.major_axis:
                dslice = dense.major_xs(idx)
                sslice = sparse.major_xs(idx)
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)

    def test_minor_xs(self):
        def _dense_comp(sparse):
            dense = sparse.to_dense()

            for idx in sparse.minor_axis:
                dslice = dense.minor_xs(idx)
                sslice = sparse.minor_xs(idx).to_dense()
                assert_frame_equal(dslice, sslice)

        _dense_comp(self.panel)