ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 27 Oct 2016 13:05:06 -0500sage vs. python integers and floats in pandas, matplotlib etc.http://ask.sagemath.org/question/35270/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'
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!
Thu, 27 Oct 2016 07:41:36 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/Answer by slelievre for <p>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:</p>
<pre><code>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'>
</code></pre>
<p>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?</p>
<p>Thanks for your help!</p>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?answer=35273#post-id-35273Python 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')Thu, 27 Oct 2016 08:06:04 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?answer=35273#post-id-35273Comment by slelievre for <p>Python modules such as matplotlib, numpy, pandas, etc, don't know about Sage integers.
So you have to feed them Python ints.</p>
<p>However, Sage's preparser automatically turns every integer in your input into a Sage integer.</p>
<p>One way to counter that is to append the letter <code>r</code> (for 'raw'), as in:</p>
<pre><code>print data_inf[:3r]
</code></pre>
<p>See the effect on ints and floats:</p>
<pre><code>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'>
</code></pre>
<p>Another way to not have preparsing on is to turn the preparser off:</p>
<pre><code>preparser(False)
</code></pre>
<p>Another way is to write your code in a <code>.py</code> file and to load that file.
That is, write this in a file, say <code>mycode.py</code>:</p>
<pre><code>import pandas as pd
fname = 'blah.xlsx'
data_inf = pd.read_excel(fname)
print data_inf[:3]
</code></pre>
<p>and then run the following:</p>
<pre><code>sage: load('mycode.py')
</code></pre>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35282#post-id-35282You can check yourself how each statement in your worksheet would be preparsed. Eg,
sage: preparse('RR.<x> = QQ[]')Thu, 27 Oct 2016 09:54:27 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35282#post-id-35282Comment by stan for <p>Python modules such as matplotlib, numpy, pandas, etc, don't know about Sage integers.
So you have to feed them Python ints.</p>
<p>However, Sage's preparser automatically turns every integer in your input into a Sage integer.</p>
<p>One way to counter that is to append the letter <code>r</code> (for 'raw'), as in:</p>
<pre><code>print data_inf[:3r]
</code></pre>
<p>See the effect on ints and floats:</p>
<pre><code>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'>
</code></pre>
<p>Another way to not have preparsing on is to turn the preparser off:</p>
<pre><code>preparser(False)
</code></pre>
<p>Another way is to write your code in a <code>.py</code> file and to load that file.
That is, write this in a file, say <code>mycode.py</code>:</p>
<pre><code>import pandas as pd
fname = 'blah.xlsx'
data_inf = pd.read_excel(fname)
print data_inf[:3]
</code></pre>
<p>and then run the following:</p>
<pre><code>sage: load('mycode.py')
</code></pre>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35278#post-id-35278Wow, 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.Thu, 27 Oct 2016 08:34:33 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35278#post-id-35278Answer by kcrisman for <p>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:</p>
<pre><code>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'>
</code></pre>
<p>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?</p>
<p>Thanks for your help!</p>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?answer=35277#post-id-35277> 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.)Thu, 27 Oct 2016 08:18:29 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?answer=35277#post-id-35277Comment by kcrisman for <blockquote>
<p>Is there a way to set up proper parsing for the worksheet at the onset</p>
</blockquote>
<p>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.)</p>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35286#post-id-35286It's because these are "pure Python" ones that we don't actually have to build an interface for - perhaps this is ironic.Thu, 27 Oct 2016 13:05:06 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35286#post-id-35286Comment by stan for <blockquote>
<p>Is there a way to set up proper parsing for the worksheet at the onset</p>
</blockquote>
<p>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.)</p>
http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35279#post-id-35279Thanks, 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.Thu, 27 Oct 2016 08:36:21 -0500http://ask.sagemath.org/question/35270/sage-vs-python-integers-and-floats-in-pandas-matplotlib-etc/?comment=35279#post-id-35279