首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >升级到熊猫版本1.4.0或1.4.1将导致调用方法.at[idx,"XXX"]生成InvalidIndexError

升级到熊猫版本1.4.0或1.4.1将导致调用方法.at[idx,"XXX"]生成InvalidIndexError
EN

Stack Overflow用户
提问于 2022-02-28 10:10:22
回答 2查看 272关注 0票数 0

我的程序在升级到python大熊猫版本1.4.0或1.4.1之后失败,有以下跟踪:

代码语言:javascript
运行
复制
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的理解并不专业。因此,我无法得到潘达斯开发团队在这个问题上的帮助。我希望能找到一个人,了解这两个版本之间的变化,潘达斯,这可以指出我的正确方向,看看回溯。有人能帮忙吗?

由于我无法创建模拟数据,下面是再现错误的最小示例:

代码语言:javascript
运行
复制
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失败。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-20 09:43:02

使用文档中的示例,at旨在获取或设置DataFrame或Series中的单个索引值。在熊猫1.4中使用索引列表中的at ( [4,5]甚至[4] )将失败(但不是更早):

代码语言:javascript
运行
复制
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获取和设置索引集合:

代码语言:javascript
运行
复制
df.loc[[4,5], 'B']
4    2
5    4
票数 1
EN

Stack Overflow用户

发布于 2022-03-10 22:14:29

同样的错误只能通过降级到熊猫版本1.3.5来解决,如果您使用的是pip:

代码语言:javascript
运行
复制
pip install pandas==1.3.5
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71293357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档