0

I want to insert values into a table (MySQL), where a field is defined as

Column('profit', DECIMAL(8, 2), nullable=False)

The values being inserted are Python floats (stored in a DataFrame, from Pandas library). When I do this with SQLAlchemy, I'm getting a warning:

Warning: Data truncated for column 'profit' at row 124

What do I have to do to avoid this warning? I don't see a way to use Python's Decimal class here because the table may contain decimals with different precision, baring that any operation on instances of this class will be very slow and far too cumbersome.

wvxvw
  • 8,089
  • 10
  • 32
  • 61
  • Then why use a `DECIMAL` in the db? Why not a `FLOAT`? – user2722968 Jul 30 '17 at 15:36
  • @user2722968 because this is how the table is defined. The definition of the table isn't under my control. – wvxvw Jul 31 '17 at 05:13
  • Then you are out of options - the floats will get truncated. – user2722968 Jul 31 '17 at 06:32
  • @user2722968 sure, that's not a problem. I just want to suppress the warning since it pollutes the log. In other words: I can convert the values in the `DataFrame` before insertion into something that won't generate the warning. I understand that the loss of precision is inevitable in this situation. To give an example: I could convert all floats to strings having representation with desired precision, but I hoped there is a way to just dismiss the warning rather than doing a massive conversion. – wvxvw Jul 31 '17 at 06:41
  • Perhaps a better question is: why are they `float`s in the first place? – univerio Aug 01 '17 at 00:07
  • @univerio that's not something I can influence: they come from some JSON-encoded request, where there are no decimal types. – wvxvw Aug 01 '17 at 06:54
  • Possible duplicate of [How to disable python warnings](https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings) – Ilja Everilä Aug 01 '17 at 07:00
  • @IljaEverilä the warning comes from SQL, not from Python. – wvxvw Aug 01 '17 at 07:01
  • Ah, sorry failed to read with thought. Still, I'd think it is translated to a python warning, or is it not? – Ilja Everilä Aug 01 '17 at 07:03
  • @IljaEverilä not that I can see, and I don't want to disable it in general. I would be interested to disable it per specific column in the database, not per Python function. – wvxvw Aug 01 '17 at 07:06
  • You can [parse JSON numbers as decimals](https://docs.python.org/2/library/json.html#json.load) (`json.loads(..., parse_float=decimal.Decimal)`). – univerio Aug 01 '17 at 16:41

0 Answers0