[SciPy-User] scikits.timeseries.tsfromtxt & guess

Pierre GM pgmdevlist@gmail....
Mon Jan 4 12:21:44 CST 2010

On Jan 4, 2010, at 12:11 PM, Tim Michelsen wrote:
> Hello,
> I first want to stress again that the tsfromtxt in the timeseries scikit is a
> real killer function.

My, thanks a lot

> Once one has understood the easyness of the "dateconverter" function it becomes
> a quick exercise to read in timeseries from ASCII files.
> As I am currently predefining a set of dateconverters for frquently used
> date-time combinations in different formats, I have the following question:
> Is it possible to integrate "ts.extras.guess_freq(dates)" into the function
> scikits.timeseries.tsfromtxt?

Probably, but I doubt it'll be very different from the current behavior. See, the dateconverter function transforms a series of strings into a unique Date, independently for each row of the input. You can't guess the frequency of an individual Date, you need several to compare their lags. That means that no matter what, you'll have to reprocess the array. I'd prefer to leave this operation up to the user...

> Currently, I would need to read a file twice: once for guessing the frequency
> based on a created list of dates and then read file to create the timeseries.

I'd first create the time series from the input, then try to guess the frequency from the DateArray

> How can I pass a frequency value to the dateconverter argument?
> Like:
> def mydateconverter(year, month, day, hour, freq='T'):
>    freq = ts.extras.guess_freq(year, month, day, hour)
>    ts_date =  ts.Date(freq, year=int(year), month=int(month), day=int(day))
>    return ts_date
> myts= ts.tsfromtxt(datafile, skiprows=1, names=None, 
>                      datecols=(1,2,3), guess_freq=True,  
>                      dateconverter=mydateconverter(freq='H'))
> I get this error then:
> TypeError: mydateconverter() takes at least 2 non-keyword arguments (0 given)

Please send a small example of datafile so that I can test wht goes wrong. If I have to guess: the line `dateconverter=mydateconverter(freq='H')` forces a call to mydateconverter without any argument (but for he frequency). Of course, that won't fly. 
What you want is to have `mydateconverter(freq='H')` callable. You should probably create a class that takes a frequency as instantiation input and that has a __call__ method, something like:

class myconverter(object)
    def __init__(freq='D'):
    def __call__(self, y,m,d,h):
        return ts.Date(self.freq, year=int(y),month=int(m),day=int(day),hour=int(h))

That way, myconverter(freq='T') becomes a valid function (you can call it).

More information about the SciPy-User mailing list