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

Juan Nunez-Iglesias jni.soma at gmail.com
Fri Feb 15 04:14:09 EST 2019


> 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]).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190215/d22e51af/attachment.html>


More information about the NumPy-Discussion mailing list