 # Question regarding NUMPY arrays slice

Hello am doing the Real Estate Analysis in the finanacial analysis with python i have an array of closing prices. Say I want to calculate the simple return: (closing price t+1 - Closing price t )/closing price t.

The code in the example for a function is

return daily_simple_ror

I understand the np.diff but i dont seem to get around the adj_closings[:-1] which for me seems to be all the array except the last digit.But the function seems to gather only one value and in the formula (closing price t+1 - Closing price t )/closing price t… it gets the closing price t. Can someone explain me the logic behind it?

1 Like

Welcome to the forum! Are you sure it says `adj_closings[:-1]`?

The difference between `adj_closings[:-1]` and `adj_closings[-1]` would be that the former returns a slice of the list except for the final index, and the latter returns just the final index.

The behaviour you’re describing would seem to be the latter? (I can’t check it myself, as I don’t have Jupyter to open the notebook on this PC…)

np diff creates an array , so we aplly the division on this array to get what we want

Hey there!
I also had this same problem and have been trying to figure it out in the last hour! here is what i have found
Since simple rate of return is just = (current value - original value) / original value) *100 , by applying np.diff u automatically create an array of the differences of all the consecutive values in ur original array( which in this case “adj_closings” - then all u need to do is to divide the difference by “adj_closings[:-1]”( which is the all of the elements in the 5th column except the last one ( which is 75.919998)- then U r done:)
I highly recommend watching this video to have full grasp on np.diff : HERE:)](Numpy Diff Simply Explained (+Bonus Video) | Finxter)
HAPPY CODING !!
BEST,
EMRE TURAN