Ask Your Question
4

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

asked 2016-10-27 14:41:36 +0100

stan gravatar image

updated 2016-10-27 16:21:03 +0100

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'  
data_inf = pd.read_excel(fname)
# 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?

Thanks for your help!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2016-10-27 15:06:04 +0100

slelievre gravatar image

updated 2016-10-27 15:07:19 +0100

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'  
data_inf = pd.read_excel(fname)
print data_inf[:3]

and then run the following:

sage: load('mycode.py')
edit flag offensive delete link more

Comments

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.

stan gravatar imagestan ( 2016-10-27 15:34:33 +0100 )edit

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

sage: preparse('RR.<x> = QQ[]')
slelievre gravatar imageslelievre ( 2016-10-27 16:54:27 +0100 )edit
0

answered 2016-10-27 15:18:29 +0100

kcrisman gravatar image

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.)

edit flag offensive delete link more

Comments

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.

stan gravatar imagestan ( 2016-10-27 15:36:21 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2016-10-27 20:05:06 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-10-27 14:41:36 +0100

Seen: 1,197 times

Last updated: Oct 27 '16