# sage vs. python integers and floats in pandas, matplotlib etc.

I have run into a problem before, where e.g. matplotlib did not like sage floats as input for axes ranges and I had to wrap them all in np.float(). Now I found a similar problem with pandas:

import pandas as pd
fname = 'blah.xlsx'
# This works:
print data_inf[:int(3)]
# This does not:
print data_inf[:3]

TypeError: cannot do slice indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [3] of <type 'sage.rings.integer.Integer'>


Is there a way to set up proper parsing for the worksheet at the onset, so that I don't have to wrap numbers and variables in the specific type needed by the respective function every time?

edit retag close merge delete

Sort by » oldest newest most voted

Python modules such as matplotlib, numpy, pandas, etc, don't know about Sage integers. So you have to feed them Python ints.

However, Sage's preparser automatically turns every integer in your input into a Sage integer.

One way to counter that is to append the letter r (for 'raw'), as in:

print data_inf[:3r]


See the effect on ints and floats:

sage: type(1)
<type 'sage.rings.integer.Integer'>
sage: type(1r)
<type 'int'>
sage: type(1.5)
<type 'sage.rings.real_mpfr.RealLiteral'>
sage: type(1.5r)
<type 'float'>


Another way to not have preparsing on is to turn the preparser off:

preparser(False)


Another way is to write your code in a .py file and to load that file. That is, write this in a file, say mycode.py:

import pandas as pd
fname = 'blah.xlsx'
print data_inf[:3]


and then run the following:

sage: load('mycode.py')

more

Wow, thanks for the comprehensive answer! What side effects would I get by setting preparser(False) for a whole worksheet? I don't always have the problem, and in Sage 6.8 the above code worked without problems, so I assume that usually the preparser (or something else) converts the numbers into the right type when passed to numpy, scipy, matplotlib etc.

( 2016-10-27 15:34:33 +0200 )edit

You can check yourself how each statement in your worksheet would be preparsed. Eg,

sage: preparse('RR.<x> = QQ[]')

( 2016-10-27 16:54:27 +0200 )edit

Is there a way to set up proper parsing for the worksheet at the onset

Since you are using worksheets, you have the option to simply use the worksheets in python mode from the start. In sagenb this is done from a dropdown menu, in Jupyter I think it's the "kernel" you use, and in SageMathCloud I think there is also a kernel or menu option. If what you are doing doesn't "really" use Sage all that much, this may be a useful workaround. (Otherwise of course the options in Samuel's answer are quite comprehensive.)

more

Thanks, this is correct, but I do have to use the sage kernel for everything else. Usually the transfer of variables between various modules works well, so I don't understand why it doesn't in some isolated cases.

( 2016-10-27 15:36:21 +0200 )edit

It's because these are "pure Python" ones that we don't actually have to build an interface for - perhaps this is ironic.

( 2016-10-27 20:05:06 +0200 )edit