【问题标题】:Last Observation Carried Forward in python with datetime使用日期时间在 python 中进行的最后一次观察
【发布时间】:2019-08-26 23:05:37
【问题描述】:

我有这个事件数据集,在检索它时只记录了更改,我希望将这些更改转换为统一的时间序列。数据以 12 小时的时间间隔记录。 retrieve_time 是一个对象,start_time 是 datetime64。

   ID        Count  retrieval_time                start_time
   100231380 70     2017-10-11T23:30:00.000+10:30 21/10/17 23:30
   100231380 70     2017-10-12T11:30:00.000+10:30 21/10/17 23:30
   100231380 72     2017-10-12T23:30:00.000+10:30 21/10/17 23:30
   100231380 72     2017-10-13T11:30:00.000+10:30 21/10/17 23:30
   100231380 73     2017-10-13T23:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-14T11:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-14T23:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-15T11:30:00.000+10:30 21/10/17 23:30
   100231380 77     2017-10-15T23:30:00.000+10:30 21/10/17 23:30
   100231380 83     2017-10-16T11:30:00.000+10:30 21/10/17 23:30
   100231380 85     2017-10-16T23:30:00.000+10:30 21/10/17 23:30
   100231380 85     2017-10-17T11:30:00.000+10:30 21/10/17 23:30
   100231380 90     2017-10-17T23:30:00.000+10:30 21/10/17 23:30
   100231380 90     2017-10-18T11:30:00.000+10:30 21/10/17 23:30
   100231380 93     2017-10-18T23:30:00.000+10:30 21/10/17 23:30
   100231380 99     2017-10-19T23:30:00.000+10:30 21/10/17 23:30
   100231380 104    2017-10-20T23:30:00.000+10:30 21/10/17 23:30
   100231380 117    2017-10-21T23:30:00.000+10:30 21/10/17 23:30

我希望能够使其保持一致,例如在最后 3 行中,从 2017 年 10 月 19 日的检索时间开始,上午 11:30 没有记录数据。我希望能够添加一行并将其替换为整行的最后一次观察。

我想输出是这样的..

   ID        Count  retrieval_time                start_time
   100231380 70     2017-10-11T23:30:00.000+10:30 21/10/17 23:30
   100231380 70     2017-10-12T11:30:00.000+10:30 21/10/17 23:30
   100231380 72     2017-10-12T23:30:00.000+10:30 21/10/17 23:30
   100231380 72     2017-10-13T11:30:00.000+10:30 21/10/17 23:30
   100231380 73     2017-10-13T23:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-14T11:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-14T23:30:00.000+10:30 21/10/17 23:30
   100231380 74     2017-10-15T11:30:00.000+10:30 21/10/17 23:30
   100231380 77     2017-10-15T23:30:00.000+10:30 21/10/17 23:30
   100231380 83     2017-10-16T11:30:00.000+10:30 21/10/17 23:30
   100231380 85     2017-10-16T23:30:00.000+10:30 21/10/17 23:30
   100231380 85     2017-10-17T11:30:00.000+10:30 21/10/17 23:30
   100231380 90     2017-10-17T23:30:00.000+10:30 21/10/17 23:30
   100231380 90     2017-10-18T11:30:00.000+10:30 21/10/17 23:30
   100231380 93     2017-10-18T23:30:00.000+10:30 21/10/17 23:30
   100231380 93     2017-10-19T11:30:00.000+10:30 21/10/17 23:30
   100231380 99     2017-10-19T23:30:00.000+10:30 21/10/17 23:30
   100231380 99     2017-10-20T11:30:00.000+10:30 21/10/17 23:30
   100231380 104    2017-10-20T23:30:00.000+10:30 21/10/17 23:30
   100231380 104    2017-10-21T11:30:00.000+10:30 21/10/17 23:30
   100231380 117    2017-10-21T23:30:00.000+10:30 21/10/17 23:30

我也想知道如何格式化检索时间和开始时间以使其相似以便能够进行比较。

而且,我想要一些通用的解决方案,因为我已经为多个事件汇总了分组数据,并且时间间隔是相同的 12 小时,但是,所有事件的检索时间和开始时间都不同。

谢谢。

【问题讨论】:

    标签: python-3.x analytics locf


    【解决方案1】:

    根据我的理解,这就是我实现上述内容的方式。 我的 csv 数据是:

    id,count,ret_time,start_time
    10022,60,2017-10-11T11:30:00.000+10:30,21/10/2017 23:30
    10023,70,2017-10-11T23:30:00.000+10:30,21/10/2017 23:30
    10024,70,2017-10-12T11:30:00.000+10:30,21/10/2017 23:30
    10025,80,2017-10-12T23:30:00.000+10:30,21/10/2017 23:30
    10026,90,2017-10-13T11:30:00.000+10:30,21/10/2017 23:30
    10027,95,2017-10-14T11:30:00.000+10:30,21/10/2017 23:30
    

    下面的脚本:

    import csv
    import time
    import datetime
    import os
    from pathlib import Path
    
    #Read csv data (my file is in a folder '/data')
    data_folder = Path(os.getcwd())
    file_path = data_folder / 'data/stack_overflow.csv'
    
    #Create list to store csv data
    csv_data = []
    
    #Read csv file
    with open(file_path) as csvFile:
        readCsv = csv.reader(csvFile, delimiter=',')
        #Skip header
        next(readCsv)
        for row in readCsv:
            #Add rows in the end of the list
            csv_data.append(row)
    
    #Transform time in string to datetime object in dict
    for row in range(len(csv_data)):
      #Convert the time to floating point milliseconds
      csv_data[row][2] = time.mktime(time.strptime(csv_data[row][2], '%Y-%m-%dT%H:%M:%S.%f%z'))
    
    #Parse the dictionary and compare difference between ret_times
    prev_time = csv_data[0][2]
    print(type(csv_data[row][2]))
    for row in range(len(csv_data)):
        #Find delta in hours (divide by seconds/hr)
        delta = (csv_data[row][2] - prev_time) / 3600
        prev_time = csv_data[row][2]
    
        #If the delta is greater than 24 hours, i.e
        #there is no value for the 12 hour difference
        #then copy the (current row - 1) and assign to a new temp list,
        #update the time to 12 hours ahead in the new list,
        #add the list item before the current row in dict 
    
        if delta > 12.0:
          #index of item that is to be copied (current row - 1)
          idx = row - 1
          #Store the value to be copied in a temp list
          temp_list = []
          temp_list = csv_data[idx].copy()
          #Add 12 hours to the time (add seconds)
          temp_list[2] = temp_list[2] + 43200
          #Add temp_list element before current row
          csv_data.insert(row, temp_list)
    
    #Shows that id: 1026 is added before 1027 as 1026 is missing the value for 11:30PM 
    print(csv_data)
    

    你可以按照同样的逻辑来转换start_time:

    csv_data[row][2] = time.mktime(time.strptime(csv_data[row][2], '%Y-%m-%dT%H:%M:%S.%f%z'))
    

    然后比较ret_time和start_time。

    希望这会有所帮助。

    【讨论】:

    • @Raj 如果这符合您的要求,您能否将其标记为您的问题的答案,以便其他人在需要时也可以参考。谢谢。
    猜你喜欢
    • 2021-06-27
    • 2021-09-25
    • 2018-12-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2011-02-16
    相关资源
    最近更新 更多