2017-07-28 74 views
0

我在由比特幣歷史的三列的CSV文件,如下所示:爲什麼這個python-panda操作返回一個錯誤?

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

我試圖讓10分鐘這樣的時間間隔的OHLC:

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' 

我是這個總的新手,甚至沒有通過文檔閱讀,我可以找出什麼是錯的。

+0

設定前data_price試'data_frame [ 「DATE_TIME」] = data_frame.to_datetime(data_frame [ 「DATE_TIME」],單位= 「S」'。我猜測熊貓不實現10位整數是秒的感覺時代,所以你需要先用'unit ='s''來轉換它 – Tony

回答

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() 
+0

它的工作原理!你能簡單解釋一下爲什麼和我做錯了什麼? –

+1

當你應用ohlc()時,它正在考慮Date_Time列作爲整數,因爲時間以秒爲單位,所以我使用** pd.to_datetime(data_frame.index,unit ='s')**將它轉換爲日期時間格式。 – Shadkhan

相關問題