[SciPy-user] calculations using the datetime information of timeseries

Tim Michelsen timmichelsen@gmx-topmail...
Tue Dec 2 16:00:25 CST 2008

Hello Pierre,
this thingy to use the datetime information really bothers me now.

>>> As a wrap-up:
>>> Try to avoid looping if you can.
>> Yes, I noticed that.
>> But I couldn't find another way to pass the individual datetimes to my
>> calculation function which expects only one value at once (i.e. it  
>> is not
>> designed to calculate full arrays).
> That might be a bottleneck. If you could modify your function so that  
> it can process arrays, you should get better results. Of course, that  
> depends on the actual function...
> When I asked whether you really needed datetime objects, I was  
> thinking about the actual datetime.datetime objects, not about objects  
> having, say, a `day` or `hour` property. If you send an example of  
> function closer to your actual need, I may be able to help you more.
I prepared an example. Maybe you have some ideas how to optimize the code.

Please find below my commented example.

### START ###

#!/usr/bin/env python

import datetime as dt
import numpy as np
import scikits.timeseries as ts

def hoy(datetime_obj):
     calculate hour of year
     mydt = datetime_obj
     year = mydt.year
     start = dt.datetime(mydt.year, 01, 01, 0)
     td = mydt - start

     seconds = td.days * 3600 * 24 + td.seconds
     hours = seconds / 3600

     return hours

def create_ts(datetime_obj):
     create a hourly series
     data = np.arange(0,8760)
     startdate = ts.Date(freq='H', datetime=datetime_obj)
     series = ts.time_series(data, freq='H', start_date=startdate)

     return series

## get a datetime object
my_datetime = dt.datetime.now()
## create time series
myseries = create_ts(my_datetime)
## calculate hoy for datetime object
my_hoy = hoy(my_datetime)
print 'my_hoy:', my_hoy

## first vectorize
hoy_vect = np.vectorize(hoy)

## calculate the hoy for each hour in the series

# 1 method: working but workaround since the main calculation is 
#           outside the time series object!!!
array_hoy = hoy_vect(myseries.dates.tolist())
series_hoy_01 = ts.time_series(array_hoy, myseries.dates)

# 2. method: desired but not working

#series_hoy_02 = hoy_vect(myseries.dates)
## this fails with the error message:
# AttributeError: 'numpy.int32' object has no attribute 'year'
# or
# AttributeError: 'int' object has no attribute 'year'

def create_dt(series):
     dt_vect = np.vectorize(dt.datetime)
     dt_ser = dt_vect(series.year, series.month, series.hour)

     return dt_ser

ser = create_dt(myseries)

series_hoy_03 = hoy_vect(dt.datetime(myseries.year, myseries.month,

### END CODE ###

Thanks in advance,

More information about the SciPy-user mailing list