influxdata / influxdb-python

Python client for InfluxDB
MIT License
1.68k stars 520 forks source link

Error setting tags using DataFrameClient #630

Open connde opened 5 years ago

connde commented 5 years ago

Hi, I'm trying to use DataFrameClient to store data into Influx, I managed to insert the data into the database, but I cannot set any tags, so I'm able to insert if I don't set a tag.

Could someone help me out? I'm sure is something simple, but since I'm new to influx I'm not being able to solve it.

I want to store currency prices (open, close, high, low, volume) data related to a symbol. I'm working with minute candles.

So basically I consume an API that return a json, after I have it on the dataframe I set the types of each column. I also clean the dataframe and eliminate possible duplications.

df['timestamp'] = pandas.to_datetime(df['timestamp'])
df = df.set_index('timestamp')  
df['open'] = pandas.to_numeric(df['open'])
df['high'] = pandas.to_numeric(df['high'])
df['low'] = pandas.to_numeric(df['low'])
df['close'] = pandas.to_numeric(df['close'])
df['volume'] = pandas.to_numeric(df['volume'])
df['trades'] = pandas.to_numeric(df['trades']) 

This works, but without tags influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test") I'm creating the tag like that as I saw some samples in the internet tags = {'symbol': df[['symbol']]}

So if I do like that I will get an error influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test", tag_columns=tags)

Traceback (most recent call last): File "c:\Workspace\algo-trading\bitmex\testInflux.py", line 61, in influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test", tag_columns=tags, field_columns=columns) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb_dataframe_client.py", line 131, in write_points field_columns=field_columns) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb_dataframe_client.py", line 264, in _convert_dataframe_to_json dataframe[tag_columns].to_dict('record'), File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\frame.py", line 2688, in getitem return self._getitem_column(key) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column return self._get_item_cache(key) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pandas\core\generic.py", line 2487, in _get_item_cache res = cache.get(item) TypeError: unhashable type: 'dict'

So I think I need to add field_columns also, tried both combinations with same above message

columns = ['timestamp', 'open', 'high', 'low', 'close', 'trades', 'volume'] 
columns = {'timestamp', 'open', 'high', 'low', 'close', 'trades', 'volume'}

influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test", tag_columns=tags, field_columns=columns)

So now I try using tags param influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test", tags=tags)

This is the error I get

Traceback (most recent call last): File "c:\Workspace\algo-trading\bitmex\testInflux.py", line 61, in influx.write_points(dataframe=df, database=dbname, protocol=protocol, time_precision='m', measurement="test", tags=tags) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb_dataframe_client.py", line 138, in write_points protocol=protocol) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb\client.py", line 490, in write_points tags=tags, protocol=protocol) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb\client.py", line 551, in _write_points protocol=protocol File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb\client.py", line 327, in write headers=headers File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\influxdb\client.py", line 286, in request raise InfluxDBClientError(response.content, response.status_code) influxdb.exceptions.InfluxDBClientError: 400: {"error":"partial write: unable to parse 'test,symbol=\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ symbol': missing fields\nunable to parse 'timestamp\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ': missing fields\nunable to parse '2017-01-01\ 00:00:00\ \ XBTUSD': missing fields\nunable to parse '2017-01-01\ 00:01:00\ \ XBTUSD': missing fields\nunable to parse '2017-01-01\ 00:02:00\ \ XBTUSD': missing fields\nunable to parse '2017-01-01\ 00:03:00\ \ XBTUSD': missing fields\nunable to parse '2017-01-01\ 00:04:00\ \

Sample when inserting without tags

select * from test name: test time close high low open symbol trades volume


1483228800000000000 968.29 968.29 968.29 968.29 XBTUSD 0 0 1483228860000000000 968.7 968.76 968.49 968.29 XBTUSD 17 12993 1483228920000000000 968.43 968.7 967.2 968.7 XBTUSD 19 73800 1483228980000000000 967.21 968 967.21 968.43 XBTUSD 4 3500 1483229040000000000 966.97 967.21 966.74 967.21 XBTUSD 17 15969

Also, is this how I should insert this type of information? index: timestamp tags: symbol fields: close, high, low, open, trades, volume

laimaretto commented 5 years ago

What version of the client and server are you using? I can insert DF into influxDB 1.3.5 with client 4.1.1 using tags. I'm facing troubles with versions 1.6.1 / 5.2.0. But provided that you stay in 1.3.5 / 4.1.1, it should work nicely ...

See this for an example: https://github.com/influxdata/influxdb-python/issues/629

connde commented 5 years ago

I have 1.6.1 / 5.2.0.

Have you tried downgrading only the python package to see if it worked?