绘制每小时时间序列数据的关键错误

问题描述 投票:0回答:1

我正在尝试按小时绘制时间序列数据。

                         col1      col2   col3      col4    col5    col6    col7
DateTime                            
2018-09-26 00:00:00     25502921    0   44.677500   0.0     0.0     0.0     0
2018-09-26 01:00:00     25502921    0   44.677500   0.0     0.0     0.0     0
2018-09-26 02:00:00     25502921    0   44.677500   0.0     0.0     0.0     0
2018-09-26 03:00:00     25502921    0   44.677500   0.0     0.0     0.0     0

我尝试使用hourly.plot(x=hourly.index, y='col1')这是我得到的以下跟踪。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-77-63f4f82f4d4e> in <module>
----> 1 hourly.plot(x=hourly.index, y='col1')

~/.local/lib/python3.6/site-packages/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
    810                 if is_integer(x) and not data.columns.holds_integer():
    811                     x = data_cols[x]
--> 812                 elif not isinstance(data[x], ABCSeries):
    813                     raise ValueError("x must be a label or position")
    814                 data = data.set_index(x)

~/.local/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2804             if is_iterator(key):
   2805                 key = list(key)
-> 2806             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   2807 
   2808         # take() does not accept boolean indexers

~/.local/lib/python3.6/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1551 
   1552         self._validate_read_indexer(
-> 1553             keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
   1554         )
   1555         return keyarr, indexer

~/.local/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1638             if missing == len(indexer):
   1639                 axis_name = self.obj._get_axis_name(axis)
-> 1640                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1641 
   1642             # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [DatetimeIndex(['2018-09-26 00:00:00', '2018-09-26 01:00:00',\n               '2018-09-26 02:00:00', '2018-09-26 03:00:00',\n               '2018-09-26 04:00:00', '2018-09-26 05:00:00',\n               '2018-09-26 06:00:00', '2018-09-26 07:00:00',\n               '2018-09-26 08:00:00', '2018-09-26 09:00:00',\n               '2018-09-26 10:00:00', '2018-09-26 11:00:00',\n               '2018-09-26 12:00:00', '2018-09-26 13:00:00',\n               '2018-09-26 14:00:00', '2018-09-26 15:00:00',\n               '2018-09-26 16:00:00', '2018-09-26 17:00:00',\n               '2018-09-26 18:00:00', '2018-09-26 19:00:00',\n               '2018-09-26 20:00:00', '2018-09-26 21:00:00',\n               '2018-09-26 22:00:00', '2018-09-26 23:00:00'],\n              dtype='datetime64[ns]', name='DateTime', freq='H')] are in the [columns]"

请帮助我找出问题所在。

python-3.x pandas time-series keyerror datetimeindex
1个回答
0
投票

删除索引,您应该这样做:

hourly.plot( y='col1')

并且熊猫将根据索引绘制y列。

© www.soinside.com 2019 - 2024. All rights reserved.