为什么这个python-panda操作返回错误?

时间:2017-07-28 18:00:07

标签: python pandas csv dataframe

我有一个CSV文件,包含三列比特币历史记录,如下所示:

1500326826,2174.000000000000,0.027612680000
1500326826,2174.000000000000,0.158374090000
1500326826,2174.000000000000,0.100000000000
1500326834,2174.000000000000,2.000000000000
...

我试图让OHLC的间隔为10分钟,如下所示:

data_frame= 
 pd.read_csv('./btcmag/raw_initial_currency_data/krakenUSD.csv',
 names=['Date_Time', 'Price', 'Volume'], index_col=0, parse_dates=True)

data_price = data_frame['Price'].resample('10Min').ohlc()

我尝试了不同的方式,例如:

data_price = data_frame.resample('10Min').ohlc()

但总是在命令行中得到这个:

   data_price = data_frame['Price'].resample('10Min').ohlc()
  File "/Users/john/.virtualenvs/btcmag/lib/python2.7/site-packages/pandas/core/generic.py", line 4729, in resample
    base=base, key=on, level=level)
  File "/Users/john/.virtualenvs/btcmag/lib/python2.7/site-packages/pandas/core/resample.py", line 969, in resample
    return tg._get_resampler(obj, kind=kind)
  File "/Users/john/.virtualenvs/btcmag/lib/python2.7/site-packages/pandas/core/resample.py", line 1091, in _get_resampler
    "but got an instance of %r" % type(ax).__name__)
TypeError: 
Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, 
but got an instance of 'Int64Index'

我是这方面的新手,甚至没有阅读我可以找出错误的文档。

1 个答案:

答案 0 :(得分:1)

我认为这会奏效。

data_frame= pd.read_csv('./btcmag/raw_initial_currency_data/krakenUSD.csv',  names=['Date_Time', 'Price', 'Volume'], index_col=0, parse_dates=True)

data_frame.index = pd.to_datetime(data_frame.index, unit='s')

data_price = data_frame['Price'].resample('10Min').ohlc()