How to fix this "Key Error:t" i got while i was writing a while loop?
How to fix this "Key Error:t" i got while i was writing a while loop?
我写了这段代码,然后我在下面写了一个错误。有一个 while 循环可以让我获取受 1000 列限制的数据,时间与我在 reapeat_rounds 变量中描述的一样多,但是当我尝试使用该循环时,"keyerror:t" raises.If我做 reapeat_rounds = 0,我没有得到这样的错误,但如果我这样做,这意味着 while 循环将无法工作,如果 while 循环工作,我得到错误请帮助我解决该错误,谢谢。
class Binance:
def __init__(self):
self.base = 'https://api.binance.com'
self.endpoints = {
"order": '/api/v3/order',
"testOrder": '/api/v3/order/test',
"allOrders": '/api/v3/allOrders',
"klines": '/api/v3/klines',
"exchangeInfo": '/api/v3/exchangeInfo'
}
self.headers = {"X-MBX-APIKEY": binance_keys['api_key']}
def GetLongerSymbolData(self, symbol:str, interval:str, limit:int=1000, end_time=False):
# Basicall, we will be calling the GetSymbolData as many times as we need
# in order to get all the historical data required (based on the limit parameter)
# and we'll be merging the results into one long dataframe.
repeat_rounds = 1
if limit > 1000:
repeat_rounds = int(limit/1000)
initial_limit = limit % 1000
if initial_limit == 0:
initial_limit = 1000
# First, we get the last initial_limit candles, starting at end_time and going
# backwards (or starting in the present moment, if end_time is False)
df = self.GetSymbolData(symbol, interval, limit=initial_limit, end_time=end_time)
while repeat_rounds > 0:
# Then, for every other 1000 candles, we get them, but starting at the beginning
# of the previously received candles.
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
df = df2.append(df, ignore_index = True)
repeat_rounds = repeat_rounds - 1
return df
Data = Binance()
m = Data.GetLongerSymbolData('BTCUSDT','1h')
print(m)
Traceback (most recent call last):
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 't'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/orhan/Desktop/DW/Data.py", line 101, in <module>
m = Data.GetLongerSymbolData('BTCUSDT','1h')
File "c:/Users/orhan/Desktop/DW/Data.py", line 47, in GetLongerSymbolData
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 2980, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 't'
你不想换行吗
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
类似
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'][0])
?
此处,[0]
应用于 'time'
字符串,这基本上只会导致字符串 't'
作为您的 DataFrame 的键。
我写了这段代码,然后我在下面写了一个错误。有一个 while 循环可以让我获取受 1000 列限制的数据,时间与我在 reapeat_rounds 变量中描述的一样多,但是当我尝试使用该循环时,"keyerror:t" raises.If我做 reapeat_rounds = 0,我没有得到这样的错误,但如果我这样做,这意味着 while 循环将无法工作,如果 while 循环工作,我得到错误请帮助我解决该错误,谢谢。
class Binance:
def __init__(self):
self.base = 'https://api.binance.com'
self.endpoints = {
"order": '/api/v3/order',
"testOrder": '/api/v3/order/test',
"allOrders": '/api/v3/allOrders',
"klines": '/api/v3/klines',
"exchangeInfo": '/api/v3/exchangeInfo'
}
self.headers = {"X-MBX-APIKEY": binance_keys['api_key']}
def GetLongerSymbolData(self, symbol:str, interval:str, limit:int=1000, end_time=False):
# Basicall, we will be calling the GetSymbolData as many times as we need
# in order to get all the historical data required (based on the limit parameter)
# and we'll be merging the results into one long dataframe.
repeat_rounds = 1
if limit > 1000:
repeat_rounds = int(limit/1000)
initial_limit = limit % 1000
if initial_limit == 0:
initial_limit = 1000
# First, we get the last initial_limit candles, starting at end_time and going
# backwards (or starting in the present moment, if end_time is False)
df = self.GetSymbolData(symbol, interval, limit=initial_limit, end_time=end_time)
while repeat_rounds > 0:
# Then, for every other 1000 candles, we get them, but starting at the beginning
# of the previously received candles.
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
df = df2.append(df, ignore_index = True)
repeat_rounds = repeat_rounds - 1
return df
Data = Binance()
m = Data.GetLongerSymbolData('BTCUSDT','1h')
print(m)
Traceback (most recent call last):
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 't'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/orhan/Desktop/DW/Data.py", line 101, in <module>
m = Data.GetLongerSymbolData('BTCUSDT','1h')
File "c:/Users/orhan/Desktop/DW/Data.py", line 47, in GetLongerSymbolData
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 2980, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\orhan\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 't'
你不想换行吗
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'[0]])
类似
df2 = self.GetSymbolData(symbol, interval, limit=1000, end_time=df['time'][0])
?
此处,[0]
应用于 'time'
字符串,这基本上只会导致字符串 't'
作为您的 DataFrame 的键。