繁体   English   中英

熊猫时间序列重采样的问题

[英]Issues with Pandas Timeseries Resample

我正在使用python 3.5.1和Pandas 0.18.0,并尝试使用此笔记本来修改财务报价数据,因为这些练习对我很感兴趣:

我对某些命令有疑问,想知道这是否归因于python和pandas的版本?

例如:

这是我正在读取的文件,具有相关的输出:

data = pd.read_csv('test30dayes2tickforpython.csv',index_col=0,        header=0,parse_dates={"Timestamp" : [0,1]})
data.dtypes
Out[80]:
 Open              float64
 High              float64
 Low               float64
 Last              float64
 Volume              int64
 NumberOfTrades      int64
 BidVolume           int64
 AskVolume           int64
dtype: object

当我尝试创建另一个这样的对象时:

ticks = data.ix[:, ['High','Volume']]
ticks

我得到NaN值:

    High    Volume
Timestamp       
2015-12-27 23:00:25.000 NaN NaN
2015-12-27 23:01:11.000 NaN NaN

但是,如果我使用列引用而不是名称,它将起作用:

ticks = data.ix[:, [1,4]]
ticks


High    Volume
Timestamp       
2015-12-27 23:00:25.000 2045.25 1
2015-12-27 23:01:11.000 2045.50 2

为什么是这样?

同样,笔记本显示了另一个创建的对象:

bars = ticks.Price.resample('1min', how='ohlc')
bars

当我尝试这个我得到这个错误:

bars = ticks.High.resample('60min', how='ohlc')
bars

1小节= ticks.High.resample('60min',how ='ohlc')
AttributeError:“ DataFrame”对象没有属性“ High”

如果我不调用“高”列,它将起作用:

bars = ticks.resample('60min', how='ohlc')
bars

FutureWarning:在.resample()中如何弃用新语法为.resample(...)。ohlc()

High    Volume
open    high    low close   open    high    low close
Timestamp                               
2015-12-27 23:00:00 2045.25 2047.75 2045.25 2045.25 1.0 7.0 1.0 5.0

请问正确的命令是什么?

我很高兴看到该笔记本可能不适用于所使用的Python / Pandas Im版本,但作为一个新手,它对我非常有用,因此希望它能在我的数据上正常工作。

列名称中存在问题spaces

print (data.columns)
Index(['Timestamp', ' Open', ' High', ' Low', ' Last', ' Volume',
       ' NumberOfTrades', ' BidVolume', ' AskVolume'],
      dtype='object')

您可以strip以下空格:

data.columns = data.columns.str.strip()
print (data.columns)
Index(['Timestamp', 'Open', 'High', 'Low', 'Last', 'Volume', 'NumberOfTrades',
       'BidVolume', 'AskVolume'],
      dtype='object')

ticks = data.ix[:, ['High','Volume']]
print (ticks.head())
      High  Volume
0  2045.25       1
1  2045.50       2
2  2045.50       2
3  2045.50       2
4  2045.50       2

现在您可以使用:

print (ticks.Price.resample('1min', how='ohlc'))

如果您不想删除空格,请在列名称中添加空格:

print (ticks[' Price'].resample('1min', how='ohlc'))

但最好将Resampler.ohlc使用,如果pandas版本高于0.18.0

print (ticks.Price.resample('1min').ohlc())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM