当日期错误时,删除数据时间轴中的间隙

2024-04-19 03:48:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用我掌握的OHLC数据来描绘钱德莱斯顿。数据来自5分钟的时间段重采样到4小时的时间段,因此周末会有巨大的差距

# Load data
subdata = pd.read_csv(
  'data/M5/EURUSD.csv',
  header = None,
  skiprows = 0,
  sep = '\t',
  names = [
    'date',
    'open',
    'high',
    'low',
    'close',
    'volume'
  ],
)
subdata['date'] = pd.to_datetime(subdata['date'])
subdata.set_index(['date'], inplace = True)

# Resample
subdata = subdata.resample('4H').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
    'volume': 'sum'
}).dropna(axis=0)

在我对数据重新采样之后,然后用Bokeh绘制数据,问题是周末的间隙。这里是我用来绘制数据的代码,并使用这个concept来解决这个问题,但仍然不起作用

fig1 = figure(x_axis_type='datetime', height=400, width=900)

# I try to add this code but still not work
fig1.xaxis.major_label_overrides = {
    i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}

wide = 12*60*60*200

inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']

fig1.segment(subdata.index, subdata['high'], subdata.index, subdata['low'], color='black')
fig1.vbar(subdata.index[inc], wide, subdata['open'][inc], subdata.close[inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata.index[dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')

show(gridplot([[fig1]]))

这里是结果 There gap on weekend

我的代码有问题吗?或者我的概念有问题吗


1条回答
网友
1楼 · 发布于 2024-04-19 03:48:50

经过反复试验,我终于找到了问题的根源。将xaxis更改为enumerate(subdata.index)时,意味着xaxis使用数字而不是日期时间。但我仍然使用datetime来绘制应该使用数字的绘图,而奇怪的事情来了。为什么bokeh仍然收到xaxis数字的xaxis datetime,这最终会造成空白和错误的绘图

为了解决这个问题,需要一个来自行的索引号。在我的例子中,index使用datetime,所以需要为index number创建一个新列,然后创建一个带有index number的绘图

# Here code to make number index
subdata['x'] = subdata.reset_index().index

fig1 = figure(x_axis_type='datetime', height=400, width=900)

fig1.xaxis.major_label_overrides = {
    i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}

wide = 0.5

inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']

fig1.segment(subdata['x'], subdata['high'], subdata['x'], subdata['low'], color='black')
fig1.vbar(subdata['x'][inc], wide, subdata['open'][inc], subdata['close'][inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata['x'][dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')

show(gridplot([[fig1]]))

Result the plot

相关问题 更多 >