def setUp(self): self.data_dict = { 'ItemA' : panel_data1(), 'ItemB' : panel_data2(), 'ItemC' : panel_data3(), 'ItemD' : panel_data1(), } self.panel = SparsePanel(self.data_dict)
def to_sparse(self, fill_value=None, kind='block'): """ Convert to SparsePanel Parameters ---------- fill_value : float, default NaN kind : {'block', 'integer'} Returns ------- y : SparseDataFrame """ from pandas.core.sparse import SparsePanel frames = dict(self.iterkv()) return SparsePanel(frames, items=self.items, major_axis=self.major_axis, minor_axis=self.minor_axis, default_kind=kind, default_fill_value=fill_value)
def test_from_dict(self): fd = SparsePanel.from_dict(self.data_dict) assert_sp_panel_equal(fd, self.panel)
class TestSparsePanel(TestCase, test_panel.SafeForLongAndSparse, test_panel.SafeForSparse): @staticmethod def assert_panel_equal(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_long(self): def _compare_with_dense(panel): slp = panel.to_long() dlp = panel.to_dense().to_long() self.assert_(np.array_equal(slp.values, dlp.values)) self.assert_(np.array_equal(slp.major_labels, dlp.major_labels)) self.assert_(np.array_equal(slp.minor_labels, dlp.minor_labels)) _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_long) self.assertRaises(Exception, self.panel.to_long, 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_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_long()) # _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)