我有一个pandas数据帧,其格式如下:
Response
Time
2018-01-14 00:00:00 201
2018-01-14 00:00:00 400
2018-01-14 00:00:00 200
2018-01-14 00:00:00 400
2018-01-14 00:00:00 200
时间是索引列。
我想获得随时间分组的响应的图表(间隔15分钟),所以我写了以下内容:
for ind, itm in enumerate(df_final['Response'].unique()):
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(key='Time',freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
ax.legend(["Response: {}".format(itm)])
这适用于折旧的TimeGrouper,上面代码中的第二行是:
ax=df_final[df_final['Response'] == item].groupby(pd.TimeGrouper(freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
但是当我运行Grouper代码时,我收到错误:
KeyError: 'The grouper name Time is not found'
我还将密钥更改为df_final.index.name,但也导致KeyError:'石斑鱼名称时间未找到'
索引是索引类型,但我将其更改为DatetimeIndex:
type(df_final.index)
pandas.core.indexes.datetimes.DatetimeIndex
我改变了索引类型并运行了:
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(key=df_final.index, freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
我得到了:
TypeError: unhashable type: 'DatetimeIndex'
我显然错过了一些东西。我在这里做错了什么?
只是为了显示df_final.index索引的结果:
DatetimeIndex(['2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
...
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00'],
dtype='datetime64[ns]', name='Time', length=48960011, freq=None)
在jezrael的帮助下进行一些调查后,看起来问题出现在情节方法中。我将代码分解为:
for ind, itm in enumerate(df_final['Response'].unique()):
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(level='Time', freq='15Min')).count()
ax.plot(kind='bar', figsize=(15,10), title="Response Codes")
并且绘图线中出现的错误是:
~/anaconda2/envs/py3env/lib/python3.6/site-packages/pandas/plotting/_core.py in __init__(self, data, kind, by, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, fig, title, xlim, ylim, xticks, yticks, sort_columns, fontsize, secondary_y, colormap, table, layout, **kwds)
98 table=False, layout=None, **kwds):
99
--> 100 _converter._WARN = False
101 self.data = data
102 self.by = by
NameError: name '_converter' is not defined
我不知道我做错了什么,或者matplotlib中是否有错误,但这是我发现自己陷入困境的位置。前一行ax显示了预期的计数和时间
答案 0 :(得分:2)
我认为你需要:
pd.Grouper(level='Time',freq='15Min')
我相信您可以将Response
列添加到groupby
,重新设定为unstack
并绘制地图:
a = df_final.groupby([pd.Grouper(level='Time',freq='15Min'), 'Response'])['Response'].count()
a.unstack().plot(kind='bar', figsize=(15,10), title="Response Codes")
答案 1 :(得分:1)
似乎是问题的matplotlib版本。当我回到2.0.2版时,我没有任何问题。只需使用以下命令卸载matplotlib 2.1.1版:
! pip uninstall -y matplotlib && pip install matplotlib==2.0.2
再次导入matplotlib,代码全部工作