Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDBNumberError on trying to insert floating point number using python boto library

Code snippet :

conn = dynamo_connect()

company = Table("companydb",connection=conn)

companyrecord = {'company-slug':'www-google-com12','founding-year':1991, 'randomlist' :[1,2,3,4,5], 'randomdict' : {'a':[1,2,3],'b':'something','randomnumber':10.55} }

company.put_item(data=companyrecord)

I am getting the following error:

File "C:\Python27\lib\site-packages\boto\dynamodb2\items.py", line 329, in prepare_full
    final_data[key] = self._dynamizer.encode(value)
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 279, in encode
    return {dynamodb_type: encoder(attr)}
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 335, in _encode_m
    return dict([(k, self.encode(v)) for k, v in attr.items()])
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 279, in encode
    return {dynamodb_type: encoder(attr)}
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 305, in _encode_n
    raise DynamoDBNumberError(msg)
boto.dynamodb.exceptions.DynamoDBNumberError: BotoClientError: Inexact numeric for `10.55`
like image 860
Rohan S Avatar asked Feb 10 '15 06:02

Rohan S


People also ask

How do you convert a decimal to a float in Python?

There is two methods: float_number = float ( decimal_number ) float_number = decimal_number * 1.0.

How do you increase float precision in Python?

There are many ways to set the precision of the floating-point values. Some of them are discussed below. Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.

Why are floating-point calculation in accurate in Python?

The floating-point calculations are inaccurate because mainly the rationals are approximating that cannot be represented finitely in base 2 and in general they are approximating numbers which may not be representable in finitely many digits in any base.


3 Answers

If you are working on a larger set and want to avoid record-wise processing to convert decimal, this would help.

from decimal import Decimal

import json

changed_data = json.loads(json.dumps(data), parse_float=Decimal)
like image 178
Subhamay Avatar answered Oct 18 '22 08:10

Subhamay


Yes There are Known issues on GitHub related to floating numbers, There may be 2 workarounds , First if you are comfortable to store 10.5 instead of 10.55, then it will just work fine I guess, The another is to store the floating value as String or integer and later modulate it while accessing.

So of you chose the string part then you could store it as '10.55' instead of 10.55 and later when you access the values from the table then you could simply use float("10.55") and you will be done.

Another method is to store it as an integer , First choose a precision value (say 2 decimal values) then you will store 10.55 as 1055(multiplied by 100, since 2 decimal precision), and while accessing it you could have simply used 1055/100.0 and you will get 10.55.

like image 36
ZdaR Avatar answered Oct 18 '22 09:10

ZdaR


Use Decimal(str(your_number)) instead. See https://github.com/boto/boto3/issues/665

like image 25
kk1957 Avatar answered Oct 18 '22 08:10

kk1957