【问题标题】:Count events per range of time in Python在 Python 中计算每个时间范围内的事件
【发布时间】:2018-02-05 10:34:55
【问题描述】:

任务是计算从 1970 年到今天 (2018-02-05) 每天发生的事件(在本例中为地震)的频率。 我有这个时间清单:

['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']

我有一个未完成的教程,有这一步

In [114]: for i in range(1,len(time)):
   dt = UTCDateTime(time[i])-UTCDateTime(time[i-1])
   dt = dt / (3600*24)
   inter_event_time.append(dt)
. . . . . :

In [115]: plt.hist(inter_event_time, bins=range(0,1000,100))
Out [115]:
(array([10, 7, 3, 5, 2, 0, 1, 2, 2]),
 array([0, 100, 200, 300, 400, 500, 600, 700, 800, 900]),
 <a list of 9 Patch objects>)

最终结果是this(x 轴是天,y 轴是发生了多少次)。我的问题是我不应该是 inter_event_times 。请帮帮我!

【问题讨论】:

  • 请避免将代码粘贴为图片,因为它不允许运行它。如果您要编辑帖子,帮助会容易得多
  • 显示第一次使用 inter_event_times 的代码——如果之前从未使用过,最好只显示整个 iPython 会话——请以文本形式显示。 Python 会显示错误。你了解plt.hist函数吗,怎么调用它,它做了什么?
  • @GalAbra 对不起!我编辑了它。
  • @RoryDaulton 问题是我不知道inter_event_time 是什么,我试图找出原因,因为教程一开始就没有完成。 plt.hist 函数用于制作时间范围(dt)内发生了多少事件的直方图。

标签: python list datetime time


【解决方案1】:

我还没有看过图片,但这很有效:

import datetime

date_string = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']

date_list = [datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ') for date in date_string]

date_start = datetime.datetime(1990, 1, 1)
date_end = datetime.datetime(2000, 1, 1)
count = 0
for date in date_list:
    if date <= date_end and date >= date_start:
        count += 1

print(count)

【讨论】:

  • 首先,感谢您的反馈!但我需要问一下,我必须每年重复计数吗?
  • “每年重复”是什么意思?您想计算给定日期之间的事件数,对吗?这就是我建议的代码的作用......
【解决方案2】:

如果您需要每天频率,这是一种方法。

import pandas as pd
from datetime import datetime
from collections import Counter

lst = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
       '1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z', etc.]

# convert to datetime & normalize, then count frequencies
counter_dict = Counter(pd.to_datetime(x).normalize().to_pydatetime() for x in lst)

# Counter({datetime.datetime(2012, 4, 11, 0, 0): 2,
#          datetime.datetime(1970, 7, 31, 0, 0): 1, etc.})

为了生成直方图,您可以像这样获得范围内的总和:

sum(counter_dict[k] for k, v in counter_dict.items() \
    if (datetime(1970, 1, 1, 0, 0) < k < datetime(1979, 12, 31, 0, 0)))  # 6

【讨论】:

    【解决方案3】:

    要重新创建您给出的直方图,您需要添加inter_event_time = [],如下所示:

    import matplotlib.pyplot as plt    
    from obspy import UTCDateTime
    
    time = [
        '1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
        '1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
        '1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
        '1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
        '1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
        '1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
        '1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
        '1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
        '2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
        '2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
        '2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
        '2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
        '2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
        '2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
        '2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
        '2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
        '2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
        '2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
        '2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']
    
    inter_event_time = []
    
    for i in range(1, len(time)):
       dt = UTCDateTime(time[i])-UTCDateTime(time[i-1])
       dt = dt / (3600*24)
       inter_event_time.append(dt)
    
    plt.hist(inter_event_time, bins=range(0,1000,100))   
    plt.show()
    

    【讨论】:

    • 感谢您的反馈,但我不认为inter_event_time 只是一个简单的[ ],我还在努力弄明白!
    • 如果您运行该脚本,它将生成您的图表。此外,如果您在线搜索,您会发现其他尝试相同的人使用了一个简单的列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多