influxdata / influxdb-python

Python client for InfluxDB
MIT License
1.69k stars 521 forks source link

Numpy integer data not recognized #621

Open fluffynukeit opened 6 years ago

fluffynukeit commented 6 years ago

I'm using 5.2.0. My data is all integer data, but it always gets inserted into the DB as float. I believe the offending line is in line_protocol.py

def _escape_value(value):
    value = _get_unicode(value)

    if isinstance(value, text_type) and value != '':
        return quote_ident(value)
    elif isinstance(value, integer_types) and not isinstance(value, bool):
        return str(value) + 'i'
    elif _is_float(value):
        return repr(value)
    return str(value)

_get_unicode is called no matter the input, and that function tries to return text. This causes the text_type check to always pass, so the integer case (and I presume also the float case) never get executed.

fluffynukeit commented 6 years ago

Hi. After some further investigation, this issue is not a problem with recognizing integer data. The above code does it correctly. It does not, however, recognize numpy integers within the integer_types variable. The fix/workaround is to first convert any numpy integers like np.uint16 to integer using int().

Example:

import influxdb.line_protocol as l
import numpy as np

b = np.frombuffer(b'\x10\x10', np.uint16) # b is an array scalar of type np.uint16
l._escape_value(b[0]) # result: '4112', note that there is no 'i' at the end
l._escape_value(int(b[0])) # result: '4112i', so will be properly inserted as integer data