A lightweight Python client for querying InfluxDB using InfluxQL (SQL-like query language) and transforming results into pandas DataFrames.
This package provides a simple client to connect to InfluxDB, execute InfluxQL queries, and easily transform the results into Python data structures including pandas DataFrames. It's designed to be simple and efficient for data analysis workflows.
- Connect to InfluxDB instances using token authentication
- Execute InfluxQL queries
- Convert query results to pandas DataFrames
- Support for JSON data format
- Efficient HTTP communication via httpx
- Python 3.8+
- httpx
- pandas
pip install -r requirements.txt
Before using this client, you need to ensure that the InfluxQL API is enabled in your InfluxDB instance. Please follow this guide: https://docs.influxdata.com/influxdb/v2/query-data/influxql/
Alternatively, try running the following setting in your Docker image:
-
Configuration File Method: Edit your influxdb configuration file (usually
influxdb.conf
) and ensure the following settings:[influxql] enabled = true
-
Environment Variable Method: Set the following environment variable when starting InfluxDB:
INFLUXDB_INFLUXQL_ENABLED=true
-
Docker Setup: If you're using Docker, add the environment variable to your docker-compose.yml or docker run command:
# docker-compose.yml example services: influxdb: image: influxdb:latest environment: - INFLUXDB_INFLUXQL_ENABLED=true
from influxdb2client import InfluxQLClient
# Configure connection details
token = "your-influxdb-token"
url = "http://localhost:8086" # Your InfluxDB URL
bucket = "your-bucket-name" # InfluxDB bucket name
# Create client instance
influx_client = InfluxQLClient(host=url, bucket=bucket, token=token)
# Execute a query and convert results to a DataFrame
query = """
SELECT last(change_percent_24) as Change,
last(price_close) as Price,
last(ticker) as ticker
FROM market24
WHERE symbol =~ /USDT/
AND time > now()-24h
AND change_percent_24 > 3
GROUP BY symbol
ORDER BY time
"""
df = influx_client.query(query).as_dataframe()
# Now you can work with the pandas DataFrame
print(df.head())
# Example: Extract unique ticker values
symbols = set(df['ticker'].values)
for symbol in symbols:
if symbol is not None:
print(symbol)
# Get query results as JSON
json_data = influx_client.query(query).as_json()
print(json_data)
The client will raise appropriate HTTP exceptions if the query fails:
try:
result = influx_client.query("SELECT * FROM nonexistent_measurement").as_dataframe()
except Exception as e:
print(f"Query failed: {e}")
from influxdb2client import InfluxQLClient
# Configuration
token = "1ssd0mS1dGZRddr52BdwST1gt5IVt-umO4MjEaENSS5kvaYogC7WaVh8L2oiAWRzT2LMAp4v1QTASMoRQUmks6yg=="
url = "http://localhost:8086"
bucket = "binance"
# Initialize client
influx_client = InfluxQLClient(host=url, bucket=bucket, token=token)
# Query data for cryptocurrencies with >3% change in the last 24h
df = influx_client.query("""
SELECT last(change_percent_24) as Change,
last(price_close) as Price,
last(ticker) as ticker
FROM market24
WHERE symbol =~ /USDT/
AND time > now()-24h
AND change_percent_24 > 3
GROUP BY symbol
ORDER BY time
""").as_dataframe()
# Print unique cryptocurrency symbols
symbols = set(df['ticker'].values)
for symbol in symbols:
if symbol is not None:
print(symbol)
- The current implementation is designed for simple query operations
- Complex queries with multiple result sets might require additional handling