我有一只熊猫MultiIndex
,我试图用它切成DataFrame
。当MultiIndex
为空时,将产生ValueError
:
(Pdb) p _ix
MultiIndex(levels=[[], [], [], []],
labels=[[], [], [], []],
names=['foo', 'bar', 'baz', 'raz'])
(Pdb) p df.index
MultiIndex(levels=[['adni'], ['123', '234'], ['M12_s1', 'M24_s1'], ['CRB', 'CRB_crop', 'PON']],
labels=[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [0, 0, 0, 0, 0]],
names=['foo', 'bar', 'baz', 'raz'])
(Pdb) p df.loc[_ix]
*** ValueError: operands could not be broadcast together with shapes (0,) (4,) (0,)
两个索引的names
相匹配,所以据我了解,这种切片应该没问题。
在某些情况下,_ix
不为空时,此方法可以正常工作。我在文档中找不到描述不支持这种空DataFrame的任何内容。我缺少明显的东西吗?
编辑:添加具体示例:
$ cat so_qc.csv
foo,bar,baz,raz,qc
pd,andrew,M24_s1,CRB,True
pd,andrew,M24_s1,CRB_crop,True
$ cat so_df.csv
foo,bar,baz,raz,value
pd,andrew,M24_s1,CRB,0.701794977111406
pd,andrew,M24_s1,CRB,0.309406238674409
$ python
qc = pd.read_csv('so_qc.csv', index_col=[0,1,2,3], squeeze=True)
df = pd.read_csv('so_df.csv', index_col=[0,1,2,3])
# This is OK
df.loc[ qc.index.intersection(df.index) ]
# When I select only False elements from `qc` (which is none of them), ValueError about broadcasting
df.loc[ qc[~qc].index.intersection(df.index) ]