John [H2O] wrote:
Here's a function I wrote to calculate hourly averages:

It seems a bit slow, however... any thoughts on how to improve it?

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) )
        vals = X[ind,1][0].T
        try:
            #hr_avg = np.sum(vals) / len(vals)
            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)


Someone else may know exactly what data you are working with and what you have imported, but I, for one?, don't.

Please show us more of the program, including the import statement(s), and some sample input data. What leads you to think is is slow?

One opportunity for improvement - take the invariant out of the while statement.

   q =  dt.datetime(*X[-1,0].timetuple()[0:4])

   while hr q:


--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to