[Tutor] using datetime and calculating hourly average

Alan Gauld alan.gauld at btinternet.com
Tue Jul 7 18:13:27 CEST 2009


"John [H2O]" <washakie at gmail.com> wrote

> The data is just x,y data where x = datetime objects from the datetime
> module. y are just floats. It is bundled in a numpy array.
>
> So the only import statements are:
>
> import datetime as dt
> import numpy as np
>
> I pass the array X, where X is a numpy array of shape [n,2] where n is 
> the
> number of points in the data.

I assume there is a good reason to use a numpy array instead of
a regular list? ie You need a numpy array elsewhere in the code?
I've never used numpy bt there is a possibility that array access
is slower than list access, but I have no idea. It just adds an extra
level of complexity thats all.

> As for your comment regarding the invariant... would it be:
> while hr q:
>

I think he meant

while hr <= q

as per your original code

>>> def calc_hravg(X):
>>>     """Calculates hourly average from input data"""
>>>
>>>     X_hr = []
>>>     minX = X[:,0].min()
>>>     hr = dt.datetime(*minX.timetuple()[0:4])
>>>
>>>     while hr <= dt.datetime(*X[-1,0].timetuple()[0:4]):
>>>         nhr = hr + dt.timedelta(hours=1)
>>>         ind = np.where( (X[:,0] > hr) & (X[:,0] < nhr) )

I have no idea what this is doing but do you really mean a bitwise
and here? You are effectively bitwise anding two boolean values
which seems odd to put it mildly...

>>>         vals = X[ind,1][0].T
>>>         try:
>>>             hr_avg = np.average(vals)
>>>         except:
>>>             hr_avg = np.nan
>>>         X_hr.append([hr,hr_avg])
>>>         hr = hr + dt.timedelta(hours=1)
>>>
>>>     return np.array(X_hr)

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list