文档文档

使用 pandas 分析数据

使用 pandas,Python 数据分析库,处理、分析和可视化存储在 InfluxDB Clustered 数据库中的数据。

pandas 是一个开源的、BSD 许可的库,为 Python 编程语言提供高性能、易于使用的数据结构和数据分析工具。

安装先决条件

本指南中的示例假设使用 Python 虚拟环境和 InfluxDB 3 influxdb3-python Python 客户端库。 有关更多信息,请参阅如何开始使用 Python 查询 InfluxDB

安装 influxdb3-python 还会安装 pyarrow 库,该库为 Apache Arrow 提供 Python 绑定。

安装 pandas

要使用 pandas,您需要安装并导入 pandas 库。

在您的终端中,使用 pip 在您的活动Python 虚拟环境中安装 pandas

pip install pandas

使用 PyArrow 将查询结果转换为 pandas

以下步骤使用 Python、influxdb3-pythonpyarrow 查询 InfluxDB 并将 Arrow 数据流式传输到 pandas DataFrame

  1. 在您的编辑器中,复制以下代码并粘贴到一个新文件中——例如,pandas-example.py

    # pandas-example.py
    
    from influxdb_client_3 import InfluxDBClient3
    import pandas
    
    # Instantiate an InfluxDB client configured for a database
    client = InfluxDBClient3(
      "https://cluster-host.com",
      database="
    DATABASE_NAME
    "
    ,
    token="
    DATABASE_TOKEN
    "
    )
    # Execute the query to retrieve all record batches in the stream # formatted as a PyArrow Table. table = client.query( '''SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' ) client.close() # Convert the PyArrow Table to a pandas DataFrame. dataframe = table.to_pandas() print(dataframe)
  2. 替换以下配置值

  3. 在您的终端中,使用 Python 解释器运行该文件

    python pandas-example.py
    

该示例调用以下方法

查看示例结果

接下来,使用 pandas 分析数据

使用 pandas 分析数据

查看数据信息和统计信息

以下示例展示了如何使用 pandas DataFrame 方法来转换和汇总存储在 InfluxDB Clustered 中的数据。

# pandas-example.py

from influxdb_client_3 import InfluxDBClient3
import pandas

# Instantiate an InfluxDB client configured for a database
client = InfluxDBClient3(
  "https://cluster-host.com",
  database="
DATABASE_NAME
"
,
token="
DATABASE_TOKEN
"
)
# Execute the query to retrieve all record batches in the stream # formatted as a PyArrow Table. table = client.query( '''SELECT * FROM home WHERE time >= now() - INTERVAL '90 days' ORDER BY time''' ) client.close() # Convert the PyArrow Table to a pandas DataFrame. dataframe = table.to_pandas() # Print information about the results DataFrame, # including the index dtype and columns, non-null values, and memory usage. dataframe.info() # Calculate descriptive statistics that summarize the distribution of the results. print(dataframe.describe()) # Extract a DataFrame column. print(dataframe['temp']) # Print the DataFrame in Markdown format. print(dataframe.to_markdown())

替换以下配置值

  • DATABASE_NAME:要查询的 InfluxDB 数据库的名称
  • DATABASE_TOKEN:对指定数据库具有读取权限的数据库令牌

降采样时间序列

pandas 库提供了用于处理时间序列数据的广泛功能。

pandas.DataFrame.resample() 方法 将数据降采样和升采样到基于时间的组——例如

# pandas-example.py

...

# Use the `time` column to generate a DatetimeIndex for the DataFrame
dataframe = dataframe.set_index('time')

# Print information about the index
print(dataframe.index)

# Downsample data into 1-hour groups based on the DatetimeIndex
resample = dataframe.resample("1H")

# Print a summary that shows the start time and average temp for each group
print(resample['temp'].mean())

查看示例结果

有关更多详细信息和示例,请参阅 pandas 文档


此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像目前一样使用它,而无需更改您的代码。

阅读更多

InfluxDB 3 开源版本现已发布公开 Alpha 版

InfluxDB 3 开源版本现已可用于 alpha 测试,根据 MIT 或 Apache 2 许可获得许可。

我们正在发布作为 alpha 版本一部分的两种产品。

InfluxDB 3 Core 是我们新的开源产品。它是用于时间序列和事件数据的最新数据引擎。 InfluxDB 3 Enterprise 是一个商业版本,它建立在 Core 的基础上,增加了历史查询功能、读取副本、高可用性、可扩展性和细粒度的安全性。

有关如何开始使用的更多信息,请查看