【问题标题】:Getting error TypeError: '>' not supported between instances of 'int' and 'str',出现错误类型错误:“int”和“str”实例之间不支持“>”,
【发布时间】:2019-07-25 12:37:54
【问题描述】:

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

【问题讨论】:

  • 我认为错误很明显。只需将两者转换为相同的类型(例如while int(date) &gt; int(from_date):,它应该可以工作。

标签: python finance cryptocurrency


【解决方案1】:

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

while int(date) > int(from_date):

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

get_df(1549638000, 1556838000)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2021-09-28
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2019-05-09
    相关资源
    最近更新 更多