python - ValueError:传递值的长度为 7,索引意味着 0
问题描述
我正在尝试使用 ccxt 从 bitmex 获取 1 分钟的开盘、高盘、低盘、收盘、成交量值。一切似乎都很好,但我不知道如何解决这个错误。我知道索引是 7,因为我要进入数据框的 OHLCcolumns 中有 7 个值。我不知道为什么它反而暗示有 0。非常感谢这让我整天头疼:(
# noinspection PyUnresolvedReferences
from datetime import datetime
# noinspection PyUnresolvedReferences
import time
# noinspection PyUnresolvedReferences
import ccxt
# noinspection PyUnresolvedReferences
import numpy as np
import pandas as pd
# noinspection PyUnresolvedReferences
from IPython.display import display, clear_output
OHLCVcolumns = ['date', 'timestamp', 'open', 'high', 'low', 'close', 'volume']
dfOHLCV = pd.DataFrame(index=[], columns=OHLCVcolumns)
bitmex = ccxt.bitmex()
def fetch_current(x):
while True:
if datetime.now().second == x:
break
time.sleep(0.5)
def fetch_mex():
listOHLCV = bitmex.fetch_ohlcv('BTC/USD',
timeframe='1m',
limit=5,
params={'reverse': True})
lst = list(listOHLCV[1])
lst.insert(0, datetime.fromtimestamp((lst[0]) / (1000 + 60 * 60 * 9 - 60)).strftime("%Y/%d/%m, %H: %M:"))
series = pd.Series(lst, index=dfOHLCV)
return listOHLCV, series
while True:
fetch_current(1)
listOHLCV, series = fetch_mex()
dfOHLCV = dfOHLCV.append(series, ignore_index=True)
clear_output(wait=True)
display(listOHLCV)
display(dfOHLCV)
fetch_current(55)
解决方案
不知道你在哪里得到错误,是在这里吗?
series = pd.Series(lst, index=dfOHLCV)
如果是这样,您可以尝试:
series = pd.Series(lst, index=OHLCVcolumns)
因为当您运行它时,索引正在引用空数据帧 dfOHLCV。
推荐阅读
- ios - 快速创建按钮
- html - 在 DiagrammeR 节点中使用 HTML 表格时对齐文本中的换行符
- c# - 在 Visual Studio 中使用 .pdb 文件调试 VB 6.0 ActiveX DLL
- handlebars.js - 根据车把中的条件跳过迭代
- ruby - 在 Ruby 中,如何保存小数点后仅 1 位的浮点值?
- mysql - 在 ORDER BY 之后得到 MySQL 中的第一项
- amazon-web-services - 使用 AWS Lambda 在 AWS Sagemaker 上执行 jupyter notebook
- java - 为什么在这个程序中不能打印 String 变量?
- android - linux mint 中 Flutter 中的 Android Studio(未安装)
- tensorflow - tf keras SparseCategoricalCrossentropy 和 sparse_categorical_accuracy 在训练期间报告错误值