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

熊猫-获取日期和当前时间之间的营业时间

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

    我知道这是一个比较常见的问题,但我只找到了一个解决方案,允许我使用本地日历和假日 businesstimedelta 图书馆。

    我当前获取两个日期列之间数据的代码 作品

    df如下(创建日期列 pd.datetime.now() :

    Index   Created Date        Updated Date        Diff Hrs    Current Date
    10086   2016-11-04 16:00:00 2016-11-11 11:38:00 35.633333   2018-05-29 10:09:11.291391
    10087   2016-11-04 16:03:00 2016-11-29 12:54:00 132.850000  2018-05-29 10:09:11.291391
    10088   2016-11-04 16:05:00 2016-11-16 08:05:00 56.916667   2018-05-29 10:09:11.291391
    10089   2016-11-04 16:17:00 2016-11-08 11:37:00 11.333333   2018-05-29 10:09:11.291391
    10090   2016-11-04 16:20:00 2016-11-16 09:58:00 57.633333   2018-05-29 10:09:11.291391
    10091   2016-11-04 16:32:00 2016-11-08 11:10:00 10.633333   2018-05-29 10:09:11.291391
    

    产生差异的工作代码 Created Date Updated Date 如下:

    import datetime
    import pytz
    import businesstimedelta
    import holidays as pyholidays
    
    workday = businesstimedelta.WorkDayRule(
        start_time=datetime.time(9),
        end_time=datetime.time(17),
        working_days=[0, 1, 2, 3, 4])
    
    
    vic_holidays = pyholidays.AU(prov='VIC')
    holidays = businesstimedelta.HolidayRule(vic_holidays)
    businesshrs = businesstimedelta.Rules([workday, holidays])
    
    def BusHrs(start, end):
        return businesshrs.difference(start,end).hours+float(businesshrs.difference(start,end).seconds)/float(3600)
    
    df['Diff Hrs'] = df.apply(lambda row: BusHrs(row['Created Date'], row['Updated Date']), axis=1)   
    

    虽然运行需要一段时间,但仍能正常工作——但是尝试根据当前时间和更新时间之间的差异(例如)创建一个新列。 df['Time Since Last Update'] = df.apply(lambda row: BusHrs(row['Current Date'], row['Updated Date']), axis=1) 失败/需要永远,我不知道为什么。

    Time Since Last Update 非常感谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ben.T    6 年前

    你需要倒过来 row['Current Date'] row['Updated Date'] 在你的 df['Time Since Last Update'] ,然后用

    df['Time Since Last Update'] = df.apply(lambda row: BusHrs(row['Updated Date'], row['Current Date']), axis=1)
    

    它应该能工作。我想 start 不能在后面 end 在函数中 businesshrs.difference 。 另外,如果您想加快代码的速度,请执行以下操作:

    def BusHrs(start, end):
        diff_businesshrs = businesshrs.difference(start,end)
        # like this you calculate only once businesshrs.difference(start,end)
        return diff_businesshrs.hours+float(diff_businesshrs.seconds)/float(3600)
    

    编辑我想我找到了一个更快的方法。因为从2016年到现在,每一行的工作时间都很长,所以我想你可以通过计算两个成功更新日期之间的工作时间来更快地进行计算。 sum 直到当前日期的部分计算。您需要两个临时列,一个列的更新日期已移位,另一个列的部分工作时间为

    df = df.sort_values('Updated Date').reset_index()
    df['Shift Date'] = df['Updated Date'].shift(-1).fillna(pd.datetime.now())
    df['BsnHrs Partial'] = df.apply(lambda row: BusHrs(row['Updated Date'], row['Shift Date']), axis=1)
    df['Time Since Last Update'] = df.apply(lambda row: df['BsnHrs Partial'][row.name:].sum(), axis=1)
    df = df.drop(['Shift Date','BsnHrs Partial'],1).set_index('index') # drop and reindex
    df = df.sort_index() #if you want to go back to the original order