Getting error TypeError: '>' not supported between instances of 'int' and 'str',

Getting error TypeError: '>' not supported between instances of 'int' and 'str',

我正在尝试从 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')

因此,如果您查看 API 响应,它会显示 TimeFrom 是 int 格式的纪元时间戳,因此为 1549638000(而非 '1549638000')。

while int(date) > int(from_date):

应该可以,或者您可以将变量作为 int 传递

get_df(1549638000, 1556838000)