Greetings everybody, so what I’m basically trying to do is collect Price data (just the Price of ‘BTCBUSD’), and store it on a sqlite database. what’s puzzling me is that it works for a while then I get this error>>>
Price
0 24757.07
Price
0 24757.04
Price
0 24756.96
Price
0 24756.95
Price
0 24756.92
Price
0 24756.87
Price
0 24756.04
Price
0 24756.04
Price
0 24754.61
Price
0 24754.61
Price
0 24754.39
Price
0 24753.52
Price
0 24753.48
Price
0 24752.89
Price
0 24752.81
Price
0 24752.61
Price
0 24750.93
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-807513b2b1a0> in <module>
8 await socket.__aenter__()
9 msg = await socket.recv()
---> 10 frame = createframe(msg)
11 frame = clean(frame)
12 frame.to_sql(PAIR, engine, if_exists='append', index=False)
<ipython-input-5-dfedda7fff50> in createframe(msg)
2 df = pd.DataFrame([msg])
3 df.columns=df.columns
----> 4 df = df.loc[:,['p']]
5 df.columns = ['Price']
6 df.Price = df.Price.astype(float)
~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
887 # AttributeError for IntervalTree get_value
888 return self.obj._get_value(*key, takeable=self._takeable)
--> 889 return self._getitem_tuple(key)
890 else:
891 # we by definition only have the 0th axis
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
1067 return self._multi_take(tup)
1068
-> 1069 return self._getitem_tuple_same_dim(tup)
1070
1071 def _get_label(self, label, axis: int):
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple_same_dim(self, tup)
773 continue
774
--> 775 retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
776 # We should never have retval.ndim < self.ndim, as that should
777 # be handled by the _getitem_lowerdim call above.
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
1111 raise ValueError("Cannot index with multidimensional key")
1112
-> 1113 return self._getitem_iterable(key, axis=axis)
1114
1115 # nested tuple slicing
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_iterable(self, key, axis)
1051
1052 # A collection of keys
-> 1053 keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
1054 return self.obj._reindex_with_indexers(
1055 {axis: [keyarr, indexer]}, copy=True, allow_dups=True
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1264 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
1265
-> 1266 self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
1267 return keyarr, indexer
1268
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1306 if missing == len(indexer):
1307 axis_name = self.obj._get_axis_name(axis)
-> 1308 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
1309
1310 ax = self.obj._get_axis(axis)
KeyError: "None of [Index(['p'], dtype='object')] are in the [columns]"
this is the Code>>Python on jupyter PS: I’m still learning I haven’t been writing code for long.
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket(PAIR)
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['p']]
df.columns = ['Price']
df.Price = df.Price.astype(float)
return df
engine = sqlalchemy.create_engine('sqlite:///BTCBUSD.db')
while True:
try:
await socket.__aenter__()
msg = await socket.recv()
frame = createframe(msg)
frame.to_sql(PAIR, engine, if_exists='append', index=False)
print(frame)
#time.sleep(.5)
except BinanceAPIException as e:
print (e)
#pass
I tried adding the the .strip()/.split() and replace(’ ‘,’’) function to try and remove the blank spaces but it still produces the same problem.it works perfectly then it stops with this error.
Any help will be greatly Appreciated.
Thank you.