OHLC聚合器不';你不能在熊猫上使用dataframe吗?

OHLC聚合器不';你不能在熊猫上使用dataframe吗?,dataframe,time-series,pandas,Dataframe,Time Series,Pandas,我不确定这是否是一个bug,或者这是出于设计——也许我遗漏了一些东西,ohlc聚合器不应该与数据帧一起工作。也许这种行为是设计出来的,因为除了索引列和价格列之外的任何数据帧都可能产生奇怪的结果?其他聚合器(mean、stdev等)使用数据帧。无论如何,我试图从这些数据中获取OHLC,而转换成timeseries似乎也不起作用 下面是一个例子: import pandas as pd rng = pd.date_range('1/1/2012', periods=1000, freq='S')

我不确定这是否是一个bug,或者这是出于设计——也许我遗漏了一些东西,ohlc聚合器不应该与数据帧一起工作。也许这种行为是设计出来的,因为除了索引列和价格列之外的任何数据帧都可能产生奇怪的结果?其他聚合器(mean、stdev等)使用数据帧。无论如何,我试图从这些数据中获取OHLC,而转换成timeseries似乎也不起作用

下面是一个例子:

import pandas as pd
rng = pd.date_range('1/1/2012', periods=1000, freq='S')

ts = pd.Series(randint(0, 500, len(rng)), index=rng)
df = pd.DataFrame(randint(0,500, len(rng)), index=rng)

ts.resample('5Min', how='ohlc') # works great
df.resample('5Min', how='ohlc') # throws a "NotImplementedError"

newts = pd.TimeSeries(df) #am I missing an index command in this line?
# the above line yields this error "TypeError: Only valid with DatetimeIndex or
  PeriodIndex"

Full notimplemented错误粘贴:
NotImplementedError回溯(最后一次调用)
/home/jeff/in()
---->1 df.重新取样('5分钟',如何进行'ohlc')
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/generic.pyc重新采样(self、rule、how、axis、fill_方法、closed、label、convention、kind、loffset、limit、base)
231填充方法=填充方法,约定=约定,
232极限=极限,基准=基准)
-->233返回取样器。重新取样(自身)
234
235 def first(自补偿):
/重新采样中的usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/tseries/resample.pyc(self,obj)
66
67如果存在(轴、日期时间索引):
--->68 rs=自重采样时间戳(obj)
69 elif isinstance(轴,周期索引):
70偏移量=至偏移量(自频率)
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/tseries/resample.pyc in_resample_timestaps(self,obj)
189如果len(grouper.binlabels)191结果=分组聚合(自聚集法)
192.其他:
193#上采样快捷方式
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc合计(self、arg、*args、**kwargs)
1538         """
1539如果isinstance(参数,基串):
->1540返回getattr(self,arg)(*args,**kwargs)
1541
1542结果={}
/ohlc中的usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc(self)
384对于多个分组,结果索引将是多索引
385         """
-->386返回自我。_cython_agg_general('ohlc'))
387
388 def n(自身,n):
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc in_cython_agg_general(仅限self、how、numeric_)
1452
1453 def_cython_agg_general(自身、方式、仅数值=真):
->1454新块=自块。_cython_agg_块(如何,仅数值块=仅数值块)
1455返回自包装块(新块)
1456
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc in_cython_agg_块(仅限self、how、numeric_)
1490值=com。确保浮动(值)
1491
->1492结果,=self.grouper.aggregate(值、方式、轴=聚合轴)
1493 NEBB=生成块(结果、块.items、块.ref\u项)
1494新的_块。追加(新)
/usr/local/lib/python2.7/dist-packages/pandas-0.9.2.dev-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc(self、values、how、axis)
730值=值。交换参数(0,轴)
731如果arity>1:
-->732升起未执行错误
733 out_shape=(self.ngroups,)+values.shape[1:]
734
未实现错误:

您可以在单个列上重新采样(因为每个列都是时间序列):

对于更大的数据帧(具有多列),我不清楚该如何输出,但也许您可以制作一个面板:

In [11]: newts = Panel(dict((col, df[col].resample('5Min', how='ohlc'))
                                for col in df.columns))

In [12]: newts[0]
Out[12]: 
                     open  high  low  close
2012-01-01 00:00:00   136   136  136    136
2012-01-01 00:05:00   462   499    0    451
2012-01-01 00:10:00   209   499    0    495
2012-01-01 00:15:00    25   499    0    344
2012-01-01 00:20:00   200   498    0    199

注意:可能有一个规范输出用于对数据帧进行重新采样,但它尚未实现?

听起来它似乎尚未实现……海登,可能就是这样。如果这是真的,我想我必须弄清楚如何正确地将我的数据帧转换成可以重新采样的时间序列。到目前为止,我在这方面也没有成功。通过使用以下命令:“ts=pd.timeseries(df[0])”将数据帧转换为timeseries,然后我可以对timeseries重新采样,我可以获得所需的结果。虽然没有直接从数据框架中进行操作那么优雅,但它现在可以工作了。我一直计划让它产生层次化的列,但看起来我还没有着手。在单个列上重新采样是我想要的。工作完美。谢谢你,海登。
In [9]: df[0].resample('5Min', how='ohlc')
Out[9]: 
                     open  high  low  close
2012-01-01 00:00:00   136   136  136    136
2012-01-01 00:05:00   462   499    0    451
2012-01-01 00:10:00   209   499    0    495
2012-01-01 00:15:00    25   499    0    344
2012-01-01 00:20:00   200   498    0    199


In [10]: type(df[0])
Out[10]: pandas.core.series.TimeSeries
In [11]: newts = Panel(dict((col, df[col].resample('5Min', how='ohlc'))
                                for col in df.columns))

In [12]: newts[0]
Out[12]: 
                     open  high  low  close
2012-01-01 00:00:00   136   136  136    136
2012-01-01 00:05:00   462   499    0    451
2012-01-01 00:10:00   209   499    0    495
2012-01-01 00:15:00    25   499    0    344
2012-01-01 00:20:00   200   498    0    199