得到错误TypeError:'int'和'str'的实例之间不支持'>',

时间:2019-07-25 12:37:54

标签: python finance cryptocurrency

我正在尝试从cryptocompare API检索历史小时数据。第一个函数检索比特币上每小时数据的最新2000个数据点。但是,在定义get_df函数中的时间后,运行它后出现错误:

<ipython-input-73-81a46125c981> in get_df(from_date, to_date)
      5     # While the earliest date returned is later than the earliest date requested, keep on querying the API
      6     # and adding the results to a list.
----> 7     while date > from_date:
      8         data = get_data(date)
      9         holder.append(pd.DataFrame(data['Data']))

TypeError: '>' not supported between instances of 'int' and 'str'
def get_data(date):
    """ Query the API for 2000 days historical price data starting from "date". """
    url = "https://min-api.cryptocompare.com/data/histohour?fsym=BTC&tsym=USD&limit=2000&toTs={}".format(date)
    r = requests.get(url)
    ipdata = r.json()
    return ipdata
ipdata = get_data('1556838000')
df = pd.DataFrame(ipdata['Data'])
def get_df(from_date, to_date):
    """ Get historical price data between two dates. """
    date = to_date
    holder = []
    # While the earliest date returned is later than the earliest date requested, keep on querying the API
    # and adding the results to a list. 
    while date > from_date:
        data = get_data(date)
        holder.append(pd.DataFrame(data['Data']))
        date = data['TimeFrom']
    # Join together all of the API queries in the list.    
    df = pd.concat(holder, axis = 0)                    
    # Remove data points from before from_date
    df = df[df['time']>from_date]                       
    # Convert to timestamp to readable date format
    df['time'] = pd.to_datetime(df['time'], unit='s')   
    # Make the DataFrame index the time
    df.set_index('time', inplace=True)                  
    # And sort it so its in time order 
    df.sort_index(ascending=False, inplace=True)        
    return df

get_df('1549638000', '1556838000')

1 个答案:

答案 0 :(得分:1)

因此,如果您查看API响应,它将显示TimeFrom是int格式的纪元时间戳,因此为1549638000(不是'1549638000')。 enter image description here

while int(date) > int(from_date):

应该可以,或者您可以将var传递为int

get_df(1549638000, 1556838000)