[Numpy-discussion] optimise operation in array with datetime objects

Timmie timmichelsen@gmx-topmail...
Wed Jan 28 16:43:52 CST 2009


Thanks!

> > ### this is the loop I would like to optimize:
> > ### looping over arrays is considered inefficient.
> > ### what could be a better way?
> > hours_array = dates_array.copy()
> > for i in range(0, dates_array.size):
> > 	hours_array[i] = dates_array[i].hour
> 
> You could try:
> np.fromiter((_.hour for _ in dates_li), dtype=np.int)
> or
> np.array([_.hour for _ in dates_li], dtype=np.int)

I used dates_li only for the preparation of example data.

So let's suppose I have the array "dates_array" returned from a
a function.

How can the last part be improved:


hours_array = dates_array.copy()
for i in range(0, dates_array.size):
	hours_array[i] = dates_array[i].hour


Or is such a loop accepable from the point of calculation efficiency?

Thanks and greetings,
Timmie





More information about the Numpy-discussion mailing list