def test_multiple_categories(self):
columns = pd.MultiIndex.from_tuples([(1, 1), (1, 2), (200, 1),
(200, 2), ('pet', ''),
('toy', '')],
names=['depth', 'iter'])
data = pd.DataFrame(data=[[1, 2, 3, 4, 'russ', 'stick'],
[5, 6, 7, 8, 'milo', 'yeti'],
[9, 10, 11, 12, 'peanut', 'stick']],
columns=columns, index=['S1', 'S2', 'S3'])
obs = _reindex_with_metadata('pet', ['pet', 'toy'], data)
exp_col = pd.MultiIndex(levels=[[1, 200, 'pet', 'toy'], [1, 2, '']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
names=['depth', 'iter'])
exp_ind = pd.Index(['milo', 'peanut', 'russ'], name='pet')
exp = pd.DataFrame(data=[[5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]],
columns=exp_col, index=exp_ind)
pdt.assert_frame_equal(exp, obs[0])
exp = pd.DataFrame(data=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
columns=exp_col, index=exp_ind)
pdt.assert_frame_equal(exp, obs[1])
obs = _reindex_with_metadata('toy', ['pet', 'toy'], data)
exp_ind = pd.Index(['stick', 'yeti'], name='toy')
exp = pd.DataFrame(data=[[5, 6, 7, 8], [5, 6, 7, 8]],
columns=exp_col, index=exp_ind)
pdt.assert_frame_equal(exp, obs[0])
exp = pd.DataFrame(data=[[2, 2, 2, 2], [1, 1, 1, 1]],
columns=exp_col, index=exp_ind)
pdt.assert_frame_equal(exp, obs[1])
评论列表
文章目录