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.

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!!


From: NumPy-Discussion <> on behalf of Juan Nunez-Iglesias <>
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]).