代码之家  ›  专栏  ›  技术社区  ›  tda

使用python中的日期列表循环24小时

  •  2
  • tda  · 技术社区  · 6 年前

    我有一个清单 np.datetime64 python中的日期:

    ['2016-12-01T02:00:00.000000000', '2016-12-01T04:00:00.000000000',
     '2016-12-01T06:00:00.000000000', '2016-12-01T08:00:00.000000000',
     '2016-12-01T10:00:00.000000000', '2016-12-01T12:00:00.000000000', 
     '2016-12-01T14:00:00.000000000', '2016-12-01T16:00:00.000000000', 
     '2016-12-01T18:00:00.000000000', '2016-12-01T20:00:00.000000000', 
     '2016-12-01T22:00:00.000000000', '2016-12-02T00:00:00.000000000', 
     '2016-12-02T02:00:00.000000000', '2016-12-02T04:00:00.000000000', 
     '2016-12-02T06:00:00.000000000', '2016-12-02T08:00:00.000000000', 
     '2016-12-02T10:00:00.000000000', '2016-12-02T12:00:00.000000000', 
     '2016-12-02T14:00:00.000000000', '2016-12-02T16:00:00.000000000', 
     '2016-12-02T18:00:00.000000000', '2016-12-02T20:00:00.000000000', 
     '2016-12-02T22:00:00.000000000', '2016-12-03T00:00:00.000000000', 
     '2016-12-03T02:00:00.000000000', '2016-12-03T04:00:00.000000000',
     '2016-12-03T06:00:00.000000000', '2016-12-03T08:00:00.000000000', 
     '2016-12-03T10:00:00.000000000', '2016-12-03T12:00:00.000000000', 
     '2016-12-03T14:00:00.000000000', '2016-12-03T16:00:00.000000000', 
     '2016-12-03T18:00:00.000000000', '2016-12-03T20:00:00.000000000', 
     '2016-12-03T22:00:00.000000000']
    

    我希望在列表中循环显示每个日历日。我试图从列表中提取每个唯一的日期(即查找最小和最大日期,并在这些日期之间创建一个日期列表),但这并不适合我要做的事情。

    我希望得到的结果是有代码允许我循环遍历列表中的每个日期/日历日,并获得与此日期对应的日期时间 :

    for each_date in date_list:
        ***get all datetimes corresponding to each_date***
    
    (loop would occur 3 times in this example)
    

    音符:

    1)迭代每个[n:n+24]或任何不起作用的方法,因为不是每天都有相同的时间步数。

    2 回复  |  直到 6 年前
        1
  •  3
  •   willeM_ Van Onsem    6 年前

    如果时间戳是 命令 ,我们可以使用 itertools.groupby 函数按相应的日期对数组元素进行分组。

    这一天可以通过 np.datetime64.astype(..., dtype='datetime64[D]') ,所以我们可以这样写:

    from numpy import datetime64
    from functools import partial
    from itertools import groupby
    
    for day, timestamps in groupby(data_array,
                                   partial(datetime64.astype, dtype='datetime64[D]')):
        # process day and timestamps
        pass

    在这里 day 是一个 datetime64[D] numpy对象(它只包含天),以及 timestamps 是一个 可迭代的 (不是列表,但我们可以将其转换为列表)对应的时间戳。 data_array 是包含初始数据的数组。

    例如:

    >>> for day, timestamps in groupby(data_array,
    ...                                partial(datetime64.astype, dtype='datetime64[D]')):
    ...     print((day, list(timestamps)))
    ... 
    (numpy.datetime64('2016-12-01'), [numpy.datetime64('2016-12-01T02:00:00.000000000'), numpy.datetime64('2016-12-01T04:00:00.000000000'), numpy.datetime64('2016-12-01T06:00:00.000000000'), numpy.datetime64('2016-12-01T08:00:00.000000000'), numpy.datetime64('2016-12-01T10:00:00.000000000'), numpy.datetime64('2016-12-01T12:00:00.000000000'), numpy.datetime64('2016-12-01T14:00:00.000000000'), numpy.datetime64('2016-12-01T16:00:00.000000000'), numpy.datetime64('2016-12-01T18:00:00.000000000'), numpy.datetime64('2016-12-01T20:00:00.000000000'), numpy.datetime64('2016-12-01T22:00:00.000000000')])
    (numpy.datetime64('2016-12-02'), [numpy.datetime64('2016-12-02T00:00:00.000000000'), numpy.datetime64('2016-12-02T02:00:00.000000000'), numpy.datetime64('2016-12-02T04:00:00.000000000'), numpy.datetime64('2016-12-02T06:00:00.000000000'), numpy.datetime64('2016-12-02T08:00:00.000000000'), numpy.datetime64('2016-12-02T10:00:00.000000000'), numpy.datetime64('2016-12-02T12:00:00.000000000'), numpy.datetime64('2016-12-02T14:00:00.000000000'), numpy.datetime64('2016-12-02T16:00:00.000000000'), numpy.datetime64('2016-12-02T18:00:00.000000000'), numpy.datetime64('2016-12-02T20:00:00.000000000'), numpy.datetime64('2016-12-02T22:00:00.000000000')])
    (numpy.datetime64('2016-12-03'), [numpy.datetime64('2016-12-03T00:00:00.000000000'), numpy.datetime64('2016-12-03T02:00:00.000000000'), numpy.datetime64('2016-12-03T04:00:00.000000000'), numpy.datetime64('2016-12-03T06:00:00.000000000'), numpy.datetime64('2016-12-03T08:00:00.000000000'), numpy.datetime64('2016-12-03T10:00:00.000000000'), numpy.datetime64('2016-12-03T12:00:00.000000000'), numpy.datetime64('2016-12-03T14:00:00.000000000'), numpy.datetime64('2016-12-03T16:00:00.000000000'), numpy.datetime64('2016-12-03T18:00:00.000000000'), numpy.datetime64('2016-12-03T20:00:00.000000000'), numpy.datetime64('2016-12-03T22:00:00.000000000')])
    

    所以在这里,我们每天都会打印一份 时间戳 ,但这是当然的 所有的选择。如示例所示,并非所有切片都具有相同的长度(最后两个切片有一个额外的元素)

    请注意 时间戳 是迭代器,如果不将其转换为列表,则在一个循环之后,迭代器将 筋疲力尽的 .

    这个 groupby 工作在线性时间内,因为每次它都检查“组键”是否与前一个元素相同,但如前所述,约束是必须对数据进行排序的。

        2
  •  1
  •   jpp    6 年前

    你可以使用 collections.defaultdict 对于O(N)溶液。你可以用熊猫来规范你的 datetime 对象,尽管这也可以通过numpy实现。

    import pandas as pd
    from collections import defaultdict
    
    d = defaultdict(list)
    
    for item in L:
        day = pd.to_datetime(item).normalize().to_datetime64()
        d[day].append(item)
    
    print(d)
    
    defaultdict(list,
                {numpy.datetime64('2016-12-01T00:00:00.000000000'):
                     [numpy.datetime64('2016-12-01T02:00:00.000000000'),
                      ...
                      numpy.datetime64('2016-12-01T22:00:00.000000000')],
                 numpy.datetime64('2016-12-02T00:00:00.000000000'):
                     [numpy.datetime64('2016-12-02T00:00:00.000000000'),
                      ...
                      numpy.datetime64('2016-12-02T22:00:00.000000000')],
                 numpy.datetime64('2016-12-03T00:00:00.000000000'):
                     [numpy.datetime64('2016-12-03T00:00:00.000000000'),
                      ...
                      numpy.datetime64('2016-12-03T22:00:00.000000000')]})
    
    推荐文章