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. As for your comment regarding the invariant... would it be: while hr q: NOT while hr not q: The latter makes more sense to me, but I'm not familiar with this approach... Thanks, john Bob Gailer wrote: > > 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 > > -- View this message in context: http://www.nabble.com/using-datetime-and-calculating-hourly-average-tp24370537p24375619.html Sent from the Python - tutor mailing list archive at Nabble.com. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor