我的程序在升级到python大熊猫版本1.4.0或1.4.1之后失败,有以下跟踪:
File "XXX.py", line XXX, in XXX
data.at[idx, "False_positives"] = "-"
File "lib/python3.9/site-packages/pandas/core/indexing.py", line 2274, in setitem
return super().setitem(key, value)
File "/python3.9/site-packages/pandas/core/indexing.py", line 2229, in setitem
self.obj._set_value(*key, value=value, takeable=self._takeable)
File "/python3.9/site-packages/pandas/core/frame.py", line 3869, in _set_value
loc = self.index.get_loc(index)
File "/python3.9/site-packages/pandas/core/indexes/range.py", line 388, in get_loc
self._check_indexing_error(key)
File "/python3.9/site-packages/pandas/core/indexes/base.py", line 5637, in _check_indexing_error
raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: Int64Index([0], dtype='int64')
此错误不会发生在熊猫版本1.3.5相同的数据上,代码也不会产生任何警告。这个bug发生在我的真实生活数据中。然而,我无法用模拟数据来重现这个bug,这可能是因为我对Pandas的理解并不专业。因此,我无法得到潘达斯开发团队在这个问题上的帮助。我希望能找到一个人,了解这两个版本之间的变化,潘达斯,这可以指出我的正确方向,看看回溯。有人能帮忙吗?
由于我无法创建模拟数据,下面是再现错误的最小示例:
conda create -y --name icescreen_env icescreen -c conda-forge -c bioconda
conda activate icescreen_env
mkdir -p ~/tmp/test_icescreen
cd ~/tmp/test_icescreen
mkdir genbank_files
i=NZ_CP026548
curl -s "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=${i}&rettype=gbwithparts&retmode=txt" > genbank_files/$i.gbk
icescreen -g ~/tmp/test_icescreen/genbank_files -o ~/tmp/test_icescreen/
head ~/tmp/test_icescreen/ICEscreen_results/results/NZ_CP026548/icescreen_detection_ME/NZ_CP026548_detected_ME.summary
rm -rf ~/tmp/test_icescreen/ICEscreen_results
conda install -c conda-forge pandas=1.4.1
icescreen -g ~/tmp/test_icescreen/genbank_files -o ~/tmp/test_icescreen/
最后一行InvalidIndexError失败。
发布于 2022-03-20 09:43:02
使用文档中的示例,at
旨在获取或设置DataFrame或Series中的单个索引值。在熊猫1.4中使用索引列表中的at
( [4,5]
甚至[4]
)将失败(但不是更早):
import pandas as pd
df = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30]],
index=[4, 5, 6], columns=['A', 'B', 'C'])
df.at[[4,5], 'B']
>InvalidIndexError: [4, 5]
相反,使用loc
获取和设置索引集合:
df.loc[[4,5], 'B']
4 2
5 4
发布于 2022-03-10 22:14:29
同样的错误只能通过降级到熊猫版本1.3.5来解决,如果您使用的是pip:
pip install pandas==1.3.5
https://stackoverflow.com/questions/71293357
复制相似问题