键是索引的熊猫石斑鱼问题

ama*_*ain 5 python python-3.x 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
Run Code Online (Sandbox Code Playgroud)

时间是索引列。

我想获得随时间(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)])
Run Code Online (Sandbox Code Playgroud)

这与折旧的 TimeGrouper 一起使用,上面代码中的第二行是:

ax=df_final[df_final['Response'] == item].groupby(pd.TimeGrouper(freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
Run Code Online (Sandbox Code Playgroud)

但是当我运行 Grouper 代码时出现错误:

KeyError: 'The grouper name Time is not found'
Run Code Online (Sandbox Code Playgroud)

我还将密钥更改为 df_final.index.name 但这也导致了 KeyError: 'The grouper name Time is not found'

该索引的类型为 index,但我将其更改为 DatetimeIndex:

type(df_final.index)

pandas.core.indexes.datetimes.DatetimeIndex
Run Code Online (Sandbox Code Playgroud)

在我更改索引类型并运行后:

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")
Run Code Online (Sandbox Code Playgroud)

我有:

TypeError: unhashable type: 'DatetimeIndex'
Run Code Online (Sandbox Code Playgroud)

我显然错过了一些东西。我在这里做错了什么?

只是为了显示索引是什么 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)
Run Code Online (Sandbox Code Playgroud)

在 jezrael 的帮助下进行一些调查后,问题似乎出在 plot 方法中。我将代码分解为:

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")
Run Code Online (Sandbox Code Playgroud)

并且情节线中发生的错误是:

~/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
Run Code Online (Sandbox Code Playgroud)

我不知道我是否做错了什么,或者 matplotlib 是否有错误,但这是我发现自己被卡住的位置。上一行 ax 按预期显示计数和时间

jez*_*ael 6

我认为你需要:

pd.Grouper(level='Time',freq='15Min')
Run Code Online (Sandbox Code Playgroud)

我相信您可以将列添加Responsegroupby,重塑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")
Run Code Online (Sandbox Code Playgroud)


ama*_*ain 1

看来是 matplotlib 版本有问题。当我回到 2.0.2 版本时,我没有遇到任何问题。只需使用以下命令卸载 matplotlib 版本 2.1.1:

! pip uninstall -y matplotlib && pip install matplotlib==2.0.2
Run Code Online (Sandbox Code Playgroud)

再次导入 matplotlib ,代码一切正常