Python Pandas重新采样,奇怪的行为

时间:2014-08-10 22:51:47

标签: python pandas

我有2个数据集(cex2.txt和cex3),我想在熊猫中重新采样。使用一个数据集,我得到预期的输出,而另一个没有。 数据集是刻度数据,格式完全相同。实际上,这2个数据集仅来自两个不同的日子。

import pandas as pd
import datetime as dt

pd.set_option ('display.mpl_style', 'default')


time_converter = lambda x: dt.datetime.fromtimestamp(float(x))
data_frame = pd.read_csv('cex2.txt', sep=';', converters={'time': time_converter})

data_frame.drop('Unnamed: 7', axis=1, inplace=True)
data_frame.drop('low', axis=1, inplace=True)
data_frame.drop('high', axis=1, inplace=True)
data_frame.drop('last', axis=1, inplace=True)

data_frame = data_frame.reindex_axis(['time', 'ask', 'bid', 'vol'], axis=1)

data_frame.set_index(pd.DatetimeIndex(data_frame['time']), inplace=True)

ask = data_frame['ask'].resample('15Min', how='ohlc')
bid = data_frame['bid'].resample('15Min', how='ohlc')
vol = data_frame['vol'].resample('15Min', how='sum')

print ask
来自cex2.txt数据集的

我得到了错误的输出:

                      open  high  low  close
1970-01-01 01:00:00   NaN   NaN  NaN    NaN
1970-01-01 01:15:00   NaN   NaN  NaN    NaN
1970-01-01 01:30:00   NaN   NaN  NaN    NaN
1970-01-01 01:45:00   NaN   NaN  NaN    NaN
1970-01-01 02:00:00   NaN   NaN  NaN    NaN
1970-01-01 02:15:00   NaN   NaN  NaN    NaN
1970-01-01 02:30:00   NaN   NaN  NaN    NaN
1970-01-01 02:45:00   NaN   NaN  NaN    NaN
1970-01-01 03:00:00   NaN   NaN  NaN    NaN
1970-01-01 03:15:00   NaN   NaN  NaN    NaN

从cex3.txt数据集我得到正确的值:

                         open      high       low     close
2014-08-10 13:30:00  0.003483  0.003500  0.003483  0.003485
2014-08-10 13:45:00  0.003485  0.003570  0.003467  0.003471
2014-08-10 14:00:00  0.003471  0.003500  0.003470  0.003494
2014-08-10 14:15:00  0.003494  0.003500  0.003493  0.003498
2014-08-10 14:30:00  0.003498  0.003549  0.003498  0.003500
2014-08-10 14:45:00  0.003500  0.003533  0.003487  0.003533
2014-08-10 15:00:00  0.003533  0.003600  0.003520  0.003587

我真的很有智慧。有谁知道为什么会这样?

编辑: 以下是数据来源: https://dl.dropboxusercontent.com/u/14055520/cex2.txt https://dl.dropboxusercontent.com/u/14055520/cex3.txt 谢谢!

0 个答案:

没有答案