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