python - Python 中的 InfluxDB 和 pandas 错误

标签 python python-3.x pandas influxdb

im following the instructions 将数据从 influx 读取到 pandas 中,但出现以下错误:

ValueError                                Traceback (most recent call last) <ipython-input-13-1e63a2e6d3db> in <module>()
----> 1 df = pd.DataFrame(AandCStation)
      2 
      3 #AandCStation['time'] # gets the name
      4 
      5 #AandCStation.values

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    328                                  dtype=dtype, copy=copy)
    329         elif isinstance(data, dict):
--> 330             mgr = self._init_dict(data, index, columns, dtype=dtype)
    331         elif isinstance(data, ma.MaskedArray):
    332             import numpy.ma.mrecords as mrecords

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _init_dict(self, data, index, columns, dtype)
    459             arrays = [data[k] for k in keys]
    460 
--> 461         return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    462 
    463     def _init_ndarray(self, values, index, columns, dtype=None, copy=False):

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)    6161   
# figure out the index, if necessary    6162     if index is None:
-> 6163         index = extract_index(arrays)    6164     else:    6165         index = _ensure_index(index)

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in extract_index(data)    6200     6201         if not indexes and not raw_lengths:
-> 6202             raise ValueError('If using all scalar values, you must pass'    6203                              ' an index')    6204 

ValueError: If using all scalar values, you must pass an index

Read DataFrame defaultdict(<class 'list'>, {'NoT/machinename':         MachineName  MachineType SensorWorking  \

这是我正在运行的代码:

client = DataFrameClient(host, port, user, password, dbname)

print("Read DataFrame")
AandCStation = client.query("""SELECT * FROM "NoT/machinename" WHERE time >= now() - 12h""")
print(AandCStation)

print(type(AandCStation))

df = pd.DataFrame(AandCStation)

这是数据:

Read DataFrame
defaultdict(<class 'list'>, {'NoT/sensor':                                       MachineName  MachineType SensorWorking  \
2018-07-16 04:11:19.912895848+00:00  Quench tank          Yes   
2018-07-16 04:11:22.961838564+00:00  Quench tank          Yes   
2018-07-16 04:11:25.872680626+00:00  Quench tank          Yes   
2018-07-16 04:11:28.850205591+00:00  Quench tank          Yes   
...                                           ...          ...           ...   
2018-07-16 16:08:05.188868516+00:00  Quench tank          Yes   
2018-07-16 16:08:08.169862344+00:00  Quench tank          Yes   
2018-07-16 16:08:11.144413930+00:00  Quench tank          Yes   
2018-07-16 16:08:14.126290232+00:00  Quench tank          Yes   
2018-07-16 16:08:17.107127232+00:00  Quench tank          Yes   
2018-07-16 16:08:20.079248843+00:00  Quench tank          Yes   

                                     TempValue  
2018-07-16 04:09:50.467145647+00:00      32.69  
2018-07-16 04:09:53.888973858+00:00      32.69  
2018-07-16 04:09:55.879811649+00:00      32.69  
2018-07-16 04:09:58.818001127+00:00      32.69  
...                                        ...  
2018-07-16 16:08:05.188868516+00:00      34.19  
2018-07-16 16:08:08.169862344+00:00      34.19  
2018-07-16 16:09:43.209347998+00:00      34.19  
2018-07-16 16:09:46.187872612+00:00      34.19  

[12233 rows x 4 columns]})
<class 'collections.defaultdict'>

有什么想法为什么我会收到错误吗?

最佳答案

我今天也遇到了同样的问题。

所以事实证明你得到了一个 DataFrames 字典,你可以 concat然后droplevel以获得所需的列。

client = DataFrameClient(host, port, user, password, dbname)

print("Read DataFrame")
AandCStation = client.query("""SELECT * FROM "NoT/machinename" WHERE time >= now() - 12h""")
AandCStation = pd.concat(AandCStation, axis=1)
AandCStation.columns = AandCStation.columns.droplevel()

print(AandCStation.head())

print(type(AandCStation))

希望这有帮助!

来源:

关于python - Python 中的 InfluxDB 和 pandas 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366465/

相关文章:

python - 无法在 Python 3 的列表理解中使用 locals() 吗?

python - 如何组合多个numpy掩码

python - 如何在 Django API 中显示结果

python - 将列表字典展平为数据框

pandas - "Can not convert a ndarray into a Tensor or Operation"在 tensorflow 中使用 pandas 数据帧

python - 为什么第 13 行 a = input() 影响第 15 行 b=input(int() 上的变量?

Python在检测数组的排名后如何删除垃圾列

python - 如何使用 eval 数据框方法在表达式中使用自定义函数?

python - 如何获取包含与索引对应的特定值的列列表作为 Pandas 数据框中的新列?

pandas - 过滤每个 ID 在特定变量中具有相同值的行 - Pandas