Reputation: 1003
I'm trying to programatically construct a pandas TimeGrouper
. A quick look at the code shows that the freq
parameter of the TimeGrouper
's __init__
method gets converted into a DateOffset
by the to_offset()
function. Furthermore, to_offset()
checks whether its parameter is an instance of DateOffset
and if true, returns it.
So, this code should work:
import pandas as pd
period = 'minute'
value = 10
time_grouper = pd.TimeGrouper(pd.DateOffset(**{period:value}))
However, I get the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pandas\tseries\resample.py", line 45, in __init__
rule = self.freq.rule_code
File "C:\Python27\lib\site-packages\pandas\tseries\offsets.py", line 217, in rule_code
raise NotImplementedError
NotImplementedError
I'm using version 0.12.0 of pandas.
What am I missing to get working instances of DateOffset
and TimeGrouper
?
Upvotes: 2
Views: 1216
Reputation: 128948
You don't need to construct an offset explicity (though you can if you want), simply
passing '10T' is enough (this is what to_offset
does, converts it to an offset object). Furthermore, you rarely need to explicity construct a
TimeGrouper(and in 0.14.0, releasing shorty), you don't need to at all. You generally just
resample``
In [5]: pd.offsets.Minute('10')
Out[5]: <10 * Minutes>
In [6]: pd.TimeGrouper(freq='10T')
Out[6]: <pandas.tseries.resample.TimeGrouper at 0x3faab90>
In [7]: pd.TimeGrouper(freq='10T').freq
Out[7]: <10 * Minutes>
In [8]: pd.TimeGrouper(freq=pd.offsets.Minute('10')).freq
Out[8]: <10 * Minutes>
You could also do this:
In [1]: values = { 'minute' : 10, 'hour' : 5 }
In [3]: [ getattr(pd.offsets,k.capitalize())(v) for k,v in values.items() ]
Out[3]: [<5 * Hours>, <10 * Minutes>]
Or this (by specifying a tuple)
In [3]: pd.TimeGrouper(freq=(5,'Min')).freq
Out[3]: <5 * Minutes>
In [4]: pd.TimeGrouper(freq=(10,'H')).freq
Out[4]: <10 * Hours>
Upvotes: 1