[Numpy-discussion] [SciPy-User] Why slicing Pandas column and then subtract gives NaN?

Paul Hobson pmhobson at gmail.com
Fri Feb 15 12:50:44 EST 2019


On Fri, Feb 15, 2019 at 5:12 AM Mike C <tmrsg11 at gmail.com> wrote:

> The original data was in CSV format. I read it in using pd.read_csv(). It
> does have column names, but no row names. I don’t think numpy reads csv
> files.
>

If you read a file into a pandas structure, it will have row labels. The
default labels are integers that correspond to the ordinal positions of the
values.

Numpy reads files.
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.loadtxt.html
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.genfromtxt.html

I prefer file IO in pandas, so I don't know which function will better
suite your needs.

And also, when I do a[2:5]-b[:3], it does not throw any “index out of
> range” error. I was able to catch that, but in both Matlab and R. You get
> an error. This is frustrating!!
>

That's a feature of python in general, not numpy in particular. Every
language has its own quirks. The more you immerse yourself in them, the
quick you'll learn to adapt to them.
-paul


>
>
> ------------------------------
> *From:* NumPy-Discussion <numpy-discussion-bounces+tmrsg11=
> gmail.com at python.org> on behalf of Juan Nunez-Iglesias <jni.soma at gmail.com
> >
> *Sent:* Friday, February 15, 2019 4:15 AM
> *To:* Discussion of Numerical Python
> *Subject:* Re: [Numpy-discussion] [SciPy-User] Why slicing Pandas column
> and then subtract gives NaN?
>
>
> I don’t have index when I read in the data. I just want to slice two
> series to the same length, and subtract. That’s it!
>
> I also don’t what numpy methods wrapped within methods. They work, but
> hard do understand.
>
> How would you do it? In Matlab or R, it’s very simple, one line.
>
>
> Why are you using pandas at all? If you want the Matlab equivalent, use
> NumPy from the beginning (or as soon as possible). I personally agree with
> you that pandas is too verbose, which is why I mostly use NumPy for this
> kind of arithmetic, and reserve pandas for advanced data table type
> functionality (like groupbys and joining on indices).
>
> As you saw yourself, a.values[1:4] - b.values[0:3] works great. If you
> read in your data into NumPy from the beginning, it’ll be a[1:4] - b[0:3]
> just like in Matlab. (Or even better: a[1:] - b[:-1]).
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190215/4f6ef6ec/attachment.html>


More information about the NumPy-Discussion mailing list