influxdata / influxdb-python

Python client for InfluxDB
MIT License
1.7k stars 522 forks source link

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). #866

Open shnabin opened 3 years ago

shnabin commented 3 years ago

I'm trying to push a data frame to InluxDB using line protocol, My code is as follows:

import pyodbc
import pandas as pd
from influxdb import DataFrameClient
##influx details
dbhost = 'localhost'
dbport = '8086'
dbuser = 'admin'
dbpasswd = ''
dbname = 'SQL_MIG'
protocol = 'line'
##sql details
cnxn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}',server='localhost\sqlexpress', database='SR', trusted_connection='yes')
crsr = cnxn.cursor()
sql = '''SELECT TimeCol,TEMP1,BATTERY_VOLTAGE,TEMP2,TEMPERATURE FROM WMS WHERE TimeCol BETWEEN ? AND ? ORDER BY Timecol asc;'''
df1 = pd.read_sql_query(sql, cnxn, index_col="TimeCol", params=['2020-01-01 00:00:00', '2020-01-01 23:59:00'])
tags = { 'tag1': df1[['TEMP1']], 'tag2': df1[['TEMP2']]}
client = DataFrameClient(dbhost, dbport, dbuser, dbpasswd, dbname)
client.write_points(df1, 'demo', tags = tags)

The error I'm receiving is as follows.

Traceback (most recent call last):
  File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\SQL_Query.py", line 37, in <module>
    client.write_points(df1, 'demo', tags = tags)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\influxdb\_dataframe_client.py", line 116, in write_points
    points = self._convert_dataframe_to_lines(
  File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\influxdb\_dataframe_client.py", line 393, in _convert_dataframe_to_lines
    [",{}={}".format(k, _escape_tag(v))
  File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\influxdb\_dataframe_client.py", line 394, in <listcomp>
    if v not in [None, ''] else ""
  File "C:\Users\admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 1329, in __nonzero__
    raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried #286 but still its not working. Kindly inform how to solve this issue.