Write pandas DF with tags to influxdb

Viewed 5232

I have this [3526 rows x 5 columns] DF, where col0 is time, col1-col3 are tags and col4 is my value.

                    0             1                  2         3      4
0     2017-09-29 22:41:51     10.2.95.5   C1195_LF470_SARF   0.0.1.1  11993
1     2017-09-29 22:41:37     10.2.52.7   CF643_RCZ70_SARM  0.0.1.16  12102
2     2017-09-29 22:41:39    10.2.102.7   C1345_BQS70_SARF  0.0.1.17  18173
3     2017-09-29 22:41:41   10.2.23.212   CN165_FS470_SAR8   0.0.0.7  23525
4     2017-09-29 22:41:38     10.2.96.4   CF832_UY570_SARM   0.0.1.4   6162

So, I want to write that DF into influxdb. I'll do ...

timeValues  = df[ ['col0','col4'] ]
tags        = { 'col1': df[['col1']], 'col2': df[['col2']], 'col3':df[['col3']] }

dbConnDF = DataFrameClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnDF.write_points(dbName, tbName, timeValues, tags = tags)

After that, I get the error

Must be DataFrame with Datetime or PeriodIndex

However, if I do insert row by row using this...

dbConnQRY = InfluxDBClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnQRY.write_points(bodyDB)

where:

bodyDB = [{
    "measurement": tbName,
    "tags":
    {
        "col1": col1,
        "col2": col2,
        "col3": col3
    },
    "time": col0,
    "fields":
    {
        "col4": col4
    }
}]

... I get no error at all. So the problem appears when I try to insert the whole DF at once.

How do I tell influxdb that col0 is my index to avoid the error?

Thanks!

1 Answers
Create an index column for dataframe
timeValues  = df[ ['col4'] ]
timeValues.index  = df[ ['col0'] ]

followed by

dbConnDF = DataFrameClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnDF.write_points(dbName, tbName, timeValues, tags = tags)

That should solve the indexing problem.

Related