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

def rate_of_return(adj_closings):

daily_simple_ror=np.diff(adj_closings)/adj_closings[:-1]

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

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