總是被基礎(chǔ)的問題折磨得非常難受,在使用常規(guī)命令adata.write('data.h5ad')時出現(xiàn)報錯:
"*/lib/python3.9/site-packages/anndata/_io/utils.py", line 109, in check_key
raise TypeError(f"{key} of type {typ} is an invalid key. Should be str.")
TypeError: 0 of type <class 'int'> is an invalid key. Should be str.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
"*/lib/python3.9/site-packages/anndata/_io/h5ad.py", line 99, in write_h5ad
write_elem(f, "var", adata.var, dataset_kwargs=dataset_kwargs)
File "*/envs/Spateo2/lib/python3.9/site-packages/anndata/_io/utils.py", line 220, in func_wrapper
raise type(e)(
TypeError: 0 of type <class 'int'> is an invalid key. Should be str.
Above error raised while writing key 'var' of <class 'h5py._hl.group.Group'> to /
Github上面其實由類似的問題討論:https://github.com/scverse/anndata/issues/628
大概看了一下,問題出在h5文件存儲時,var和obs的columns不可以為數(shù)值
我的數(shù)據(jù):

修改之前.png
然后把列名改一下就解決問題了
sc.var.columns=['Gene']
修改之后
反正都是一些平常很難遇到的bug,偶爾就會出來讓人抓狂一下。解決方法不一定萬能,歡迎討論~