计算 datetime/numpy 中给出的每小时事件数
Count number of events per hour given in datetime/numpy
假设我有一个 timestamp
的数组(例如 datetime
或 numpy.datetime64
)。此外,每个条目代表一个事件。我想按小时对事件进行分类;即计算每小时发生了多少事件。有直接的方法吗?一些 Numpy/Pandas 魔法?或者唯一的方法是按照建议 here
将 timestamp
转换为秒并存储秒
如果您正在使用 Pandas,并且您的时间序列具有 DateTime 索引,则可以使用 resample
方法和 how='count'
。例如,下面的rng
是时间戳的范围,ts
是一系列以rng
为索引的值。 (对于您的计算,时间序列中的值无关紧要。)在此示例中,每小时有 360 "events",因此除最后一个小时外,每个小时的预期结果为 360。
创建一些示例数据:
In [71]: import pandas as pd
In [72]: rng = pd.date_range('1/1/2011', periods=10000, freq='10S')
In [73]: ts = pd.Series(np.random.randint(0, 5, len(rng)), index=rng)
In [74]: ts
Out[74]:
2011-01-01 00:00:00 2
2011-01-01 00:00:10 0
2011-01-01 00:00:20 1
2011-01-01 00:00:30 4
2011-01-01 00:00:40 1
2011-01-01 00:00:50 1
2011-01-01 00:01:00 2
2011-01-01 00:01:10 0
2011-01-01 00:01:20 3
2011-01-01 00:01:30 4
2011-01-01 00:01:40 2
2011-01-01 00:01:50 4
2011-01-01 00:02:00 1
2011-01-01 00:02:10 0
2011-01-01 00:02:20 4
...
2011-01-02 03:44:10 2
2011-01-02 03:44:20 0
2011-01-02 03:44:30 3
2011-01-02 03:44:40 0
2011-01-02 03:44:50 0
2011-01-02 03:45:00 4
2011-01-02 03:45:10 3
2011-01-02 03:45:20 2
2011-01-02 03:45:30 0
2011-01-02 03:45:40 1
2011-01-02 03:45:50 0
2011-01-02 03:46:00 2
2011-01-02 03:46:10 0
2011-01-02 03:46:20 2
2011-01-02 03:46:30 2
Freq: 10S, Length: 10000
使用resample
方法统计每小时的事件数。第一个参数 'H'
表示我们正在按小时重新采样。
In [75]: ts.resample('H', how='count')
Out[75]:
2011-01-01 00:00:00 360
2011-01-01 01:00:00 360
2011-01-01 02:00:00 360
2011-01-01 03:00:00 360
2011-01-01 04:00:00 360
2011-01-01 05:00:00 360
2011-01-01 06:00:00 360
2011-01-01 07:00:00 360
2011-01-01 08:00:00 360
2011-01-01 09:00:00 360
2011-01-01 10:00:00 360
2011-01-01 11:00:00 360
2011-01-01 12:00:00 360
2011-01-01 13:00:00 360
2011-01-01 14:00:00 360
2011-01-01 15:00:00 360
2011-01-01 16:00:00 360
2011-01-01 17:00:00 360
2011-01-01 18:00:00 360
2011-01-01 19:00:00 360
2011-01-01 20:00:00 360
2011-01-01 21:00:00 360
2011-01-01 22:00:00 360
2011-01-01 23:00:00 360
2011-01-02 00:00:00 360
2011-01-02 01:00:00 360
2011-01-02 02:00:00 360
2011-01-02 03:00:00 280
Freq: H, dtype: int64
假设我有一个 timestamp
的数组(例如 datetime
或 numpy.datetime64
)。此外,每个条目代表一个事件。我想按小时对事件进行分类;即计算每小时发生了多少事件。有直接的方法吗?一些 Numpy/Pandas 魔法?或者唯一的方法是按照建议 here
timestamp
转换为秒并存储秒
如果您正在使用 Pandas,并且您的时间序列具有 DateTime 索引,则可以使用 resample
方法和 how='count'
。例如,下面的rng
是时间戳的范围,ts
是一系列以rng
为索引的值。 (对于您的计算,时间序列中的值无关紧要。)在此示例中,每小时有 360 "events",因此除最后一个小时外,每个小时的预期结果为 360。
创建一些示例数据:
In [71]: import pandas as pd
In [72]: rng = pd.date_range('1/1/2011', periods=10000, freq='10S')
In [73]: ts = pd.Series(np.random.randint(0, 5, len(rng)), index=rng)
In [74]: ts
Out[74]:
2011-01-01 00:00:00 2
2011-01-01 00:00:10 0
2011-01-01 00:00:20 1
2011-01-01 00:00:30 4
2011-01-01 00:00:40 1
2011-01-01 00:00:50 1
2011-01-01 00:01:00 2
2011-01-01 00:01:10 0
2011-01-01 00:01:20 3
2011-01-01 00:01:30 4
2011-01-01 00:01:40 2
2011-01-01 00:01:50 4
2011-01-01 00:02:00 1
2011-01-01 00:02:10 0
2011-01-01 00:02:20 4
...
2011-01-02 03:44:10 2
2011-01-02 03:44:20 0
2011-01-02 03:44:30 3
2011-01-02 03:44:40 0
2011-01-02 03:44:50 0
2011-01-02 03:45:00 4
2011-01-02 03:45:10 3
2011-01-02 03:45:20 2
2011-01-02 03:45:30 0
2011-01-02 03:45:40 1
2011-01-02 03:45:50 0
2011-01-02 03:46:00 2
2011-01-02 03:46:10 0
2011-01-02 03:46:20 2
2011-01-02 03:46:30 2
Freq: 10S, Length: 10000
使用resample
方法统计每小时的事件数。第一个参数 'H'
表示我们正在按小时重新采样。
In [75]: ts.resample('H', how='count')
Out[75]:
2011-01-01 00:00:00 360
2011-01-01 01:00:00 360
2011-01-01 02:00:00 360
2011-01-01 03:00:00 360
2011-01-01 04:00:00 360
2011-01-01 05:00:00 360
2011-01-01 06:00:00 360
2011-01-01 07:00:00 360
2011-01-01 08:00:00 360
2011-01-01 09:00:00 360
2011-01-01 10:00:00 360
2011-01-01 11:00:00 360
2011-01-01 12:00:00 360
2011-01-01 13:00:00 360
2011-01-01 14:00:00 360
2011-01-01 15:00:00 360
2011-01-01 16:00:00 360
2011-01-01 17:00:00 360
2011-01-01 18:00:00 360
2011-01-01 19:00:00 360
2011-01-01 20:00:00 360
2011-01-01 21:00:00 360
2011-01-01 22:00:00 360
2011-01-01 23:00:00 360
2011-01-02 00:00:00 360
2011-01-02 01:00:00 360
2011-01-02 02:00:00 360
2011-01-02 03:00:00 280
Freq: H, dtype: int64