Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TypeError: cannot do positional indexing on <class 'pandas.core.indexes.numeric.Int64Index'>

Tags:

python

pandas

Apologies if this is a basic question, but I'm getting a Type Error, while trying to access values in a pandas dataframe.

The error is:

TypeError: cannot do positional indexing on < class 'pandas.core.indexes.numeric.Int64Index'> with these indexers [1] of < type 'sage.rings.integer.Integer'>

The code is:

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]

Most likely I just don't understand how to use iloc correctly; can anyone help please?

like image 514
phufbv Avatar asked Oct 17 '25 22:10

phufbv


1 Answers

As the error message notes, you're not using a standard Python integer with iloc but rather, something from Sage. So pandas doesn't recognise it as being a Python integer.

So if you have done something like

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.

And if you're running it as a .sage script and not a Python script, then postfix an r to prevent it being converted to a Sage type:

df.iloc[1r]

See this answer from the sagemath forums for more details.

like image 180
aneroid Avatar answered Oct 19 '25 12:10

aneroid