尝试合并多个数据框时,如何解决“ ValueError:如果使用所有标量值,则必须传递索引”

时间:2019-02-12 12:31:43

标签: python pandas dataframe append trading

我正在尝试从Bitfinex交易所获取并存储所有历史1分钟蜡烛数据。尝试将新数据帧追加到现有数据帧时,我收到此错误“ ValueError:如果使用所有标量值,则必须传递索引”,尽管在构造函数中传递了索引。

在这里尝试过解决方案-在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:如果使用所有标量值,则必须传递索引”错误,尽管尝试使用非标量值或传递索引。

1 个答案:

答案 0 :(得分:0)

                                <controls:DynamicSelectorContentControl DockPanel.Dock="Top"
                                            ContentTemplateSelector="{StaticResource AgeGenderSelector}"
                                            ListenToProperties="Gender, Age"                        
                                            Content="{Binding .}"/>

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