熊猫 - 以不等间隔绘制事件

时间:2014-12-17 21:35:37

标签: python matplotlib pandas ipython-notebook

我有一个表示事件日志的日期时间对象列表:

 [datetime.datetime(2014, 12, 16, 0, 18, 12),
  datetime.datetime(2014, 12, 16, 0, 18, 27),
  datetime.datetime(2014, 12, 16, 0, 18, 27),
  datetime.datetime(2014, 12, 16, 0, 19, 9),
  datetime.datetime(2014, 12, 16, 0, 19, 39),
  datetime.datetime(2014, 12, 16, 0, 19, 49),
  datetime.datetime(2014, 12, 16, 0, 20, 2),
  datetime.datetime(2014, 12, 16, 0, 20, 19),
  datetime.datetime(2014, 12, 16, 0, 20, 47),
  ...
  datetime.datetime(2014, 12, 16, 6, 23, 43),
  datetime.datetime(2014, 12, 16, 6, 25, 45)]

如何创建每秒事件数量的情节?例如。价值应该是:

  • 1表示datetime.datetime(2014,12,16,0,18,12)
  • 0表示datetime.datetime(2014,12,16,0,18,13) - datetime.datetime(2014,12,16,0,18,26)
  • 2 for datetime.datetime(2014,12,16,0,18,27)

我试过这样的事情:

pd.Series([1 for _ in xrange(len(events_list))], index=events_list).plot()

和此:

df = pd.DataFrame({'ts': t, 'value': 1} for t in events_list)
df.pivot_table(index='ts', columns='value', aggfunc=len, fill_value=0).plot()

显然我得错了结果:

Events count

我可以要求引导我完成这个吗?

1 个答案:

答案 0 :(得分:7)

您可能希望使用'value_counts'来计算特定时间事件的实例数,然后重新采样数据帧以填充na,就像这样,

import pandas as pd
import datetime
events = [datetime.datetime(2014, 12, 16, 0, 18, 12),
  datetime.datetime(2014, 12, 16, 0, 18, 27),
  datetime.datetime(2014, 12, 16, 0, 18, 27),
  datetime.datetime(2014, 12, 16, 0, 19, 9),
  datetime.datetime(2014, 12, 16, 0, 19, 39),
  datetime.datetime(2014, 12, 16, 0, 19, 49),
  datetime.datetime(2014, 12, 16, 0, 20, 2),
  datetime.datetime(2014, 12, 16, 0, 20, 19),
  datetime.datetime(2014, 12, 16, 0, 20, 47),
  datetime.datetime(2014, 12, 16, 6, 23, 43),
  datetime.datetime(2014, 12, 16, 6, 25, 45)]
df = pd.DataFrame ({'ts' : events})
df2 = df.ts.value_counts()
df2 = df2.resample('s').fillna(0)
print (df2.head(30))

这应该产生,

2014-12-16 00:18:12    1
2014-12-16 00:18:13    0
2014-12-16 00:18:14    0
2014-12-16 00:18:15    0
2014-12-16 00:18:16    0
2014-12-16 00:18:17    0
2014-12-16 00:18:18    0
2014-12-16 00:18:19    0
2014-12-16 00:18:20    0
2014-12-16 00:18:21    0
2014-12-16 00:18:22    0
2014-12-16 00:18:23    0
2014-12-16 00:18:24    0
2014-12-16 00:18:25    0
2014-12-16 00:18:26    0
2014-12-16 00:18:27    2
2014-12-16 00:18:28    0
2014-12-16 00:18:29    0
2014-12-16 00:18:30    0
2014-12-16 00:18:31    0
2014-12-16 00:18:32    0
2014-12-16 00:18:33    0
2014-12-16 00:18:34    0
2014-12-16 00:18:35    0
2014-12-16 00:18:36    0
2014-12-16 00:18:37    0
2014-12-16 00:18:38    0
2014-12-16 00:18:39    0
2014-12-16 00:18:40    0
2014-12-16 00:18:41    0
Freq: S, dtype: float64