任务:编写一个用于更改编码时间范围的函数。
程序主体:
import pandas as pd
import numpy as np
from future_functions import *
# Load CSV Data
data = pd.read_csv('Data/EURUSDHours.csv')
data.columns = ['Date','open','high','low','close','AskVol']
data = data.set_index(pd.to_datetime(data.Date))
data = data[['open','high','low','close','AskVol']]
prices = data.drop_duplicates(keep=False)
hkaprices = prices.copy()
hkaprices['Symbol'] = 'SYMB'
HKA = OHLCresample(hkaprices,'15H')
,其中future_functions
是具有功能的附件。
函数本身:
def OHLCresample(DataFrame,TimeFrame,column='ask'):
grouped = DataFrame.groupby('Symbol')
if np.any(DataFrame.columns == 'Ask'):
if column == 'ask':
ask = grouped['Ask'].resample(TimeFrame).ohlc()
askVol = grouped['AskVol'].resample(TimeFrame).count()
resampled = pd.DataFrame(ask)
resampled['AskVol'] = askVol
elif column == 'bid':
bid = grouped['Bid'].resample(TimeFrame).ohlc()
bidVol = grouped['BidVol'].resample(TimeFrame).count()
resampled = pd.DataFrame(bid)
resampled['BidVol'] = bidVol
else:
raise ValueError('Column must be a string. Either ask or bid')
elif np.any(DataFrame.columns == 'close'):
open = grouped['open'].resample(TimeFrame).ohlc()
close = grouped['close'].resample(TimeFrame).ohlc()
high = grouped['high'].resample(TimeFrame).ohlc()
low = grouped['low'].resample(TimeFrame).ohlc()
askVol = grouped['AskVol'].resample(TimeFrame).ohlc()
resampled = pd.DataFrame(open)
resampled['high'] = high
resampled['low'] = low
resampled['close'] = close
resampled['AskVol'] = askVol
resampled = resampled.dropna()
return resampled
我收到一个错误:
KeyError:'AskVol'
ValueError:传递的项目数错误4,展示位置表示1
可以通过以下链接获取数据集: https://nofile.io/f/Q9AKjGbSUHd/EURUSDHours.csv
我知道问题出在维数上,但我不知道如何解决。