Reputation: 23
I have a multi-dimensional array that is:
d.shape = (744, 1, 288, 315)
The first dimension is hours -- so I want to subset this array such that I only get the daytime hours, so hours 10-17 through each time 24-hour period.
I have been subsetting by creating a list:
start = 10
end = 17
d_daily = np.array([d[i*24+start:i*24+end] for i in range(31)])
but it's slow.
I feel like there should be a quick way for me to do a subset, something like:
d[start:(len(d)-(24-end)):24] # basically, take these 7 steps over and over until the end of the array
However, this gives me an array that is (31, 1, 288, 315) as opposed to the (217,1,288,315) output I would expect.
It is important that I keep the order of the data as well...
I know this is very simple, but I would appreciate your help!
Thanks
Upvotes: 0
Views: 445
Reputation: 897
What about this, using modular arithmetic:
import numpy as np
arr = np.zeros((744, 1, 288, 315))
start = 10
end = 17
idx = np.arange(744) % 24
bool_arr = np.logical_and(idx >= 10, idx < 17)
d_daily = arr[bool_arr, :, :, :]
I'm using an array of zeros as an example, and this will include the hours from [10, 17) (i.e. 17 excluded).
Upvotes: 1
Reputation: 1784
Seems fairly simple as you told, can you try this?
d[10*31:17*31,:,:,:]
Upvotes: 1