AttributeError:模块“pandas”没有属性“Int64Index”

问题描述 投票:0回答:2

我有最新版本的pandas,2.0.1。

这个简单的代码:

import pandas
pandas.Int64Index

给出错误:

AttributeError: module 'pandas' has no attribute 'Int64Index'

我也尝试使用

pip
conda
卸载并重新安装 pandas 重新启动 Jupyter Notebook,但仍然出现错误。

python pandas attributeerror
2个回答
5
投票

2.0.1 是 pandas 2.0 重大更新之后的次要版本,删除了旧的已弃用代码,例如

Int64Index
。从 2.0 更改日志来看,Index 现在可以保存 numpy numeric dtypes

Int64Index、UInt64Index 和 Float64Index 在 pandas 中已弃用 版本 1.4 现已删除。相反应该使用索引 直接,现在可以接受所有 numpy 数字 dtypes,即 int8/ int16/int32/int64/uint8/uint16/uint32/uint64/float32/float64 数据类型

当主要版本中缺少某些内容时,请务必检查变更日志。

相当于

Int64Index
的是(导入numpy后)

pd.Index([1, 2, 3], dtype=np.int64)

0
投票

在我想将数据帧保存为文件之前收到错误。所以我的解决方法是避免错误: 将三行代码放在要将数据帧保存到文件之前。但将其保存到文件当然是可选的(这就是为什么我将 # 放在前面)。

df["row_id"] = df.index + 1
df.reset_index(drop=True, inplace=True)
df.set_index("row_id", inplace = True)
#df.to_file("C:\\...")

请注意,索引列已重命名为“row_id”,并且它从 1 而不是 0 开始计数。

© www.soinside.com 2019 - 2024. All rights reserved.