When attempting to merge multiple dataframes, how to resolve "ValueError: If using all scalar values, you must pass an index"

When attempting to merge multiple dataframes, how to resolve "ValueError: If using all scalar values, you must pass an index"

我正在尝试从 Bitfinex 交易所获取并存储所有历史 1 分钟蜡烛数据。尝试将新数据帧附加到现有数据帧时,尽管在构造函数中传递了索引,但我仍收到此错误 "ValueError: If using all scalar values, you must pass an index"。

已尝试此处的解决方案 - 在 DataFrame 构造函数中传递索引: Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index"。它可能很简单,但没有运气。

# Example: https://api-pub.bitfinex.com/v2/candles/trade:1m:tBTCUSD/hist?limit=100&start=1549086300000&end=1549174500000
# Params: timeframe, ticker, number of candles, MS start, MS end
# Note: parameter "end" seems to be unnecessary.
# JSON: [[MTS, OPEN, CLOSE, HIGH, LOW, VOLUME],]

import json
import time
import datetime
import requests
import pandas as pd

url = 'https://api-pub.bitfinex.com/v2/'

# Return dataframe of all historical 1m candles

def get_candles_all(symbol):
    symbol = symbol
    limit = 5000
    tf = '1m'
    targettime = (time.time() - 120) * 1000
    start = get_genesis_timestamp(symbol)
    df = get_candles_period('1m', symbol, limit, start)
    while df.index[-1] <= targettime:
        start = df.index[-1] # reset start to last timestamp
        newdata = pd.DataFrame(get_candles_period('1m', symbol, limit, start), index=[0])
        result = df.append(newdata)
        df = result 
    return df


# Return timestamp-indexed dataframe of requested timeframe candles

def get_candles_period(tf, symbol, limit, start):
    symbol = symbol
    response = requests.get(url +"candles/trade:" + tf + ':t' + symbol + '/hist?limit=' + str(limit) + '&start=' + str(start) + '&sort=1').json()
    df = pd.DataFrame(response)
    df.columns = ["MS", "Open", "High", "Low", "Close", "Vol"]
    df.set_index("MS", inplace=True) 
return df

# Return timestamp of first available 1 min candle of given asset

def get_genesis_timestamp(symbol):
    symbol = symbol
    response = requests.get(url + "candles/trade:1m:t" + symbol + '/hist?limit=1&sort=1').json()
    df = pd.DataFrame(response)
    df.columns = ["MS", "Open", "High", "Low", "Close", "Vol"]
    df.set_index("MS", inplace=True) 
    timestamp = df.index[0]
return timestamp

symbol = "ETHUSD" 
get_candles_all(symbol)

我希望 get_candles_all() 方法迭代地将 "newdata" 附加到 "df",直到 df 的最终索引(时间戳)在目标时间的 2 分钟内。

尽管使用非标量值或传递索引进行了各种尝试,但仍继续出现 "ValueError: If using all scalar values, you must pass an index" 错误。

df.set_index(["MS"], inplace=True) 

df = pd.DataFrame(response,index=[value])