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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With