使用python的 influxdb、pandas、pymysql模块,将influxdb的时序数据,抽取到myl中。
使用influxdb模块中的DataframeClient初始化一个连接实例,然后通过实例的quey()方法,执行influxQL查询,查询需要的数据,结果集会被自动初始化为一个DataFrame类。但是当各模块版本与数据库版本等匹配不好时,会出现各类异常问题。
本次在执行查询后,结果集初始化为DataFrame类时报错,详细报错如下。
Traceback (most recent call last):
File "E:\ReportData\import-influxdb-to-mysql.py", line 163, in <module>
data_proc()
File "E:\ReportData\import-influxdb-to-mysql.py", line 147, in data_proc
df = client.query(sql)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\influxdb\_dataframe_client.py", line 202, in query
return self._to_dataframe(results, dropna,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\influxdb\_dataframe_client.py", line 222, in _to_dataframe
df.time = pd.to_datetime(df.time)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\tools\datetimes.py", line 1112, in to_datetime
values = convert_listlike(arg._values, format)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\tools\datetimes.py", line 488, in _convert_listlike_datetimes
return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\tools\datetimes.py", line 519, in _array_strptime_with_fallback
result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
File "strptime.pyx", line 534, in pandas._libs.tslibs.strptime.array_strptime
File "strptime.pyx", line 355, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data "2024-04-23T06:30:07Z" doesn't match format "%Y-%m-%dT%H:%M:%S.%f%z", at position 473. You might want to try:
- passing `format` if your strings have a consistent format;
- passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
- passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.
最终解决方案:
DataFramClient对象的query()方法中传入参数,指定结果集合 timestamp的格式类型。
client.query(sql,epoch='ns')。
query方法详细解释,可参考 API Documentation — InfluxDB 5.3.1 documentation