我正在尝试以编程方式构造pandas 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版本。
要获得
DateOffset
和TimeGrouper
的工作实例,我缺少什么? 最佳答案
您无需构造偏移量显式(尽管您可以,但可以),只需
传递'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>
关于python - 如何构造一个带有DateOffset的TimeGrouper?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23757344/