'numpy.float64' object has no attribute 'translate' Inserting value to Mysql in Python -


import dataset db = dataset.connect(....) table = db[...] 

when try insert value mysql table, error occurred.

sample value inserting table:

print("buy", ticker, price, date, otype, osize) buy aapl 93.4357142857 2016-05-12 market 200 data = dict(order_side='buy',ticker=ticker, price=price,                              order_date= date, order_type = otype, volume = osize ) table.insert(data) 

error message:

traceback (most recent call last):    file "<ipython-input-3-b7ab0c98f72f>", line 1, in <module>     runfile('c:/users/swigeluser/documents/github/trading/strategies/strat_ma_exectest.py', wdir='c:/users/swigeluser/documents/github/trading/strategies')    file "c:\users\swigeluser\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile     execfile(filename, namespace)    file "c:\users\swigeluser\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile     exec(compile(f.read(), filename, 'exec'), namespace)    file "c:/users/swigeluser/documents/github/trading/strategies/strat_ma_exectest.py", line 69, in <module>     ma_stra(start_length=7,end_length=10,start_date=date(2016,5,12),end_date=date(2016,6,18))    file "c:/users/swigeluser/documents/github/trading/strategies/strat_ma_exectest.py", line 66, in ma_stra     table.insert(data1)    file "c:\users\swigeluser\anaconda3\lib\site-packages\dataset\persistence\table.py", line 87, in insert     res = self.database.executable.execute(self.table.insert(row))    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute     return meth(self, multiparams, params)    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection     return connection._execute_clauseelement(self, multiparams, params)    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement     compiled_sql, distilled_params    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context     context)    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1405, in _handle_dbapi_exception     util.reraise(*exc_info)    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise     raise value    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context     context)    file "c:\users\swigeluser\anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute     cursor.execute(statement, parameters)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\cursors.py", line 164, in execute     query = self.mogrify(query, args)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\cursors.py", line 143, in mogrify     query = query % self._escape_args(args, conn)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\cursors.py", line 123, in _escape_args     return dict((key, conn.literal(val)) (key, val) in args.items())    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\cursors.py", line 123, in <genexpr>     return dict((key, conn.literal(val)) (key, val) in args.items())    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\connections.py", line 821, in literal     return self.escape(obj, self.encoders)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\connections.py", line 814, in escape     return escape_item(obj, self.charset, mapping=mapping)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\converters.py", line 27, in escape_item     val = encoder(val, mapping)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode     return u"'%s'" % _escape_unicode(value)    file "c:\users\swigeluser\anaconda3\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode     return value.translate(_escape_table)  attributeerror: 'numpy.float64' object has no attribute 'translate' 

what have caused error? how solve this. seems entered code here,thus have type lots of none sense in order submit.

my price , date variable comes dataframe:

for in range(len(all_tickers)-1-len(current_date),len(all_tickers)-1):     price = all_tickers[str(length) + 'day_ma'][i]     date = all_tickers['date'][i+1] 

according answer below, problem should type of price np.float64, how can convert variable type float?

your library tries format provided arguments format mysql understand. so, checks type of each argument, determine how input should formatted.

however, since lib doesn't knows numpy.float64, fallbacks default encoder, happens 1 strings (unicode). here relevent piece of code.

def escape_item(val, charset, mapping=none):     if mapping none:         mapping = encoders     encoder = mapping.get(type(val))      # fallback default when no encoder found     if not encoder:         try:             encoder = mapping[text_type]         except keyerror:             raise typeerror("no default type converter defined")      if encoder in (escape_dict, escape_sequence):         val = encoder(val, charset, mapping)     else:         val = encoder(val, mapping)     return val 

this encoder, assuming input indeed string, tries call translate() method on string. but, since method isn't defined float64, error.

you should try convert float64 regular float.

or, can create own encoder, , add in encoders dict used default mapping of python types encoder. if you're going use lot lib float64, may worth doing.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -