optimise operation in array with datetime objects
Hello, I have an array of datetime objects. What is the most efficient way of creating a new array with only the hours or minutes out of it? Here is an example: ### imports import numpy as np import datetime as dt ### create some data d = dt.datetime.now() dates_li = [] count = 0 for i in range(0, 24): count = count +1 fact = count * 3600 date_new = d + dt.timedelta(0, fact) print date_new dates_li.append(date_new) ### the array with datetime objects dates_array = np.array(dates_li) ### 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 hours_array = hours_array.astype('int') #### Thanks in advance for any hints! Kind regards, Timmie
On Jan 28, 2009, at 3:56 PM, Timmie wrote:
### 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)
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
On Jan 28, 2009, at 5:43 PM, Timmie wrote:
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.
Just use dates_array instead of dates_li, then.
hours_array = dates_array.copy() for i in range(0, dates_array.size): hours_array[i] = dates_array[i].hour
* What's the point of making a copy of dates_array ? dates_array is a ndarray of object, right ? And you want to take the hours, so you should have an ndarray of integers for hours_array. * The issue I have with this part is that you have several calls to __getitem__ at each iteration. It might be faster to use create hours_array as a block: hours_array=np.array([_.hour for _ in dates_array], dtype=np.int)
participants (2)
-
Pierre GM
-
Timmie