Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

InfluxDBClientError: 400 writing df points #558

Closed
jpsaraiva opened this issue Jan 18, 2018 · 2 comments
Closed

InfluxDBClientError: 400 writing df points #558

jpsaraiva opened this issue Jan 18, 2018 · 2 comments

Comments

@jpsaraiva
Copy link

jpsaraiva commented Jan 18, 2018

Hello,

I'm in need of some help with inserting a df into influxdb.

My code:

    print("Create pandas DataFrame")
    df = pd.read_csv('../var/demo.csv')

    print df
    df.index = pd.Series([dt.datetime.now()] * len(df))
    print df

    print("Write DataFrame with Tags")
    tags = {'COLUMN1': df['COLUMN1'], 'COLUMN2': df['COLUMN2']}
    client.write_points(df, tbName, tags=tags, protocol='json')

Output:

Create pandas DataFrame
COLUMN1 COLUMN2
0 line1 value1
1 line2 value2
COLUMN1 COLUMN2
2018-01-18 17:05:12.859 line1 value1
2018-01-18 17:05:12.859 line2 value2
Write DataFrame with Tags

On write_points I get the exception:

influxdb.exceptions.InfluxDBClientError: 400: {"error":"partial write:\nunable to parse 'testdb,COLUMN1=2018-01-18\ 17:05:12.859\ \ \ \ line1': missing fields\nunable to parse '2018-01-18\ 17:05:12.859\ \ \ \ line2': missing fields\nunable to parse 'Name:\
COLUMN1\,\ dtype:\ object,COLUMN2=2018-01-18\ 17:05:12.859\ \ \ \ value1': missing fields\nunable to parse '2018-01-18\ 17:05:12.859\ \ \ \ value2': missing fields\nunable to parse 'testdb,COLUMN1=2018-01-18\ 17:05:12.859\ \ \ \ line1': missing field
s\nunable to parse '2018-01-18\ 17:05:12.859\ \ \ \ line2': missing fields\nunable to parse 'Name:\ COLUMN1\,\ dtype:\ object,COLUMN2=2018-01-18\ 17:05:12.859\ \ \ \ value1': missing fields\nunable to parse '2018-01-18\ 17:05:12.859\ \ \ \ value2':
missing fields"}

If i run write_points without tags: client.write_points(df, tbName, protocol='json')
Execution completed but only one line is inserted.

Without setting the index I just get:
TypeError: Must be DataFrame with DatetimeIndex or PeriodIndex.

.csv file is just 3 lines:

COLUMN1,COLUMN2
line1,value1
line2,value2

I searched around, but no similar issues, #549 has the same error but different context.

Using python 2.7 and influxdb 5.0

>python --version
Python 2.7.14

>pip show influxdb
Name: influxdb
Version: 5.0.0
Summary: InfluxDB client
Home-page: https://github.com/influxdb/influxdb-python
Author: UNKNOWN
Author-email: UNKNOWN
License: MIT License
Location: c:\python27\lib\site-packages
Requires: requests, python-dateutil, six, pytz
@AndyMender
Copy link

AndyMender commented Jan 19, 2018

I'm not a Python InfluxDB client developer, but it seems to me like you're missing the "fields" dict in your JSON. The JSON should have the following structure:

{ 
  "measurement": "the name of your measurements table",
  "tags": {"tag1": "value1", ...},
  "fields": {"field1": "value1", ...},
  "time": "timestamp in RFC3339 format"
}

The "time" timestamp is optional (if absent, the current time string is used) and so is the "tags" dict, but "fields" and "measurement" are mandatory. The traceback even gives you a hint :).

@jpsaraiva
Copy link
Author

Hello Andy,

Thanks for your reply.

I found similar information at http://influxdb-python.readthedocs.io/en/latest/examples.html on the tutorials - basic example. But for tutorials - pandas, field values are not specified like that, only the dataframe and tags are passed into write_points function.

Reading on issue, #362 , using tag_columns parameter instead of tags solved the issue:
client.write_points(df, tbName, tag_columns=['COLUMN1'], protocol='json')

I did not find any documentation supporting this, but it works!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants