我试图以编程方式构建一只大熊猫TimeGrouper
。快速查看代码会显示freq
TimeGrouper
方法的__init__
参数会被DateOffset
函数转换为to_offset()
。此外,to_offset()
检查其参数是否为DateOffset
的实例,如果为true,则返回它。
所以,这段代码应该有效:
import pandas as pd
period = 'minute'
value = 10
time_grouper = pd.TimeGrouper(pd.DateOffset(**{period:value}))
但是,我得到以下例外:
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
我正在使用 0.12.0 版本的pandas。
为了获得DateOffset
和TimeGrouper
的工作实例,我缺少什么?
答案 0 :(得分:1)
您不需要构建偏移明确(尽管您可以,如果您愿意),简单地说
传递“10T”就足够了(这是to_offset
所做的,将其转换为偏移对象). 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>
你也可以这样做:
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>]
或者这(通过指定一个元组)
In [3]: pd.TimeGrouper(freq=(5,'Min')).freq
Out[3]: <5 * Minutes>
In [4]: pd.TimeGrouper(freq=(10,'H')).freq
Out[4]: <10 * Hours>