尝试合并多个数据帧时,如何解决“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"

查看:35
本文介绍了尝试合并多个数据帧时,如何解决“ValueError: If using all scalar values, you must pass an index"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch and store all historical 1 minute candle data from Bitfinex exchange. When attempting to append new dataframes to an existing one, I get this error "ValueError: If using all scalar values, you must pass an index", despite passing an index in constructor.

在这里尝试了解决方案 - 在 DataFrame 构造函数中传递一个索引:从变量中的值构造pandas DataFrame给出"ValueError:如果使用所有标量值,则必须传递索引".它可能很简单,但没有运气.

Have tried the solution here - to pass an index in the DataFrame constructor: Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index". Its probably something very simple, but have had no luck.

# 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 分钟内.

I expect the get_candles_all() method to append "newdata" to "df" iteratively until the final index (timestamp) of df is within 2 mins of targettime.

尽管多次尝试使用非标量值或传递索引,但仍继续出现ValueError:如果使用所有标量值,则必须传递索引"错误.

Continued "ValueError: If using all scalar values, you must pass an index" error despite various attempts to either use non-scalar values, or pass an index.

推荐答案

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

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

这篇关于尝试合并多个数据帧时,如何解决“ValueError: If using all scalar values, you must pass an index"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆