Python TypeError:无法在<class'pandas.core.indexes.numeric.int64index'=“”>上进行位置索引

时间:2018-10-16 23:31:56

标签: python pandas

很抱歉,如果这是一个基本问题,但是在尝试访问pandas数据框中的值时遇到类型错误。

错误是:

  

TypeError:无法使用<类型'sage.rings.integer.Integer'>

的这些索引器[1]在<类'pandas.core.indexes.numeric.Int64Index'>上进行位置索引

代码是:

import pandas as pd
df = pd.DataFrame({ 'A' : 1.,
            'B' : pd.Timestamp('20130102'),
            'C' : pd.Series(1,index=list(range(4)),dtype='float32')})
print df.iloc[1]

很可能我只是不了解如何正确使用iloc。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我尝试运行您的代码,但由于使用了Python 3,因此更改了最后一行。在这里没有看到问题。

print(df.iloc[1])

此输出:

A                      1
B    2013-01-02 00:00:00
C                      1
Name: 1, dtype: object

答案 1 :(得分:0)

如错误消息所示,您不是在iloc中使用标准Python整数,而是在Sage中使用某些东西。因此,熊猫无法将其识别为Python整数。

所以,如果您做了类似的事情

si = sage.all.Integer(1)  # or loading a sage script
# ... some lines of pandas
df.iloc[si]  # won't work
df.iloc[int(si)]  # casts from a Sage type to a Python type.

如果您将其作为.sage脚本而不是Python脚本运行,请后缀r以防止其转换​​为Sage类型:

df.iloc[1r]

有关更多详细信息,请参见this answer from the sagemath forums