【Python】在pandas 的 dateframe 中计算时间差

from datetime import datetime
import pandas as pd

df_info = pd.DataFrame([['2022-06-25 9:10:11','2022-06-25 10:14:15'],
['2022-06-25 11:10:11','2022-06-25 15:14:15'],['2022-06-25 12:10:11','2022-06-25 16:25:15']],columns=['join_time','leaving_time'])
df_info['join_time'] = pd.to_datetime(df_info['join_time'],infer_datetime_format=True)
df_info['leaving_time'] = pd.to_datetime(df_info['leaving_time'],infer_datetime_format=True)

# timeStra,timeStrb 皆为字符串对象时间;n 为转换为分、小时、天需要保留几位小数
def getTimeDiff(timeStra, timeStrb, n):
    t1 = str(timeStra)
    t1 = datetime.strptime(t1, "%Y-%m-%d %H:%M:%S")
    t2 = str(timeStrb)
    t2 = datetime.strptime(t2, "%Y-%m-%d %H:%M:%S")
    day_cnt = (t2-t1).days
    second_cnt = (t2-t1).seconds
    all_seconds = day_cnt*24*60*60 + second_cnt
    all_minutes = round(all_seconds/60, n)
    all_hours = round(all_minutes/60, n)
    all_days = round(all_hours/24, n)
    return all_minutes

def cnt_all_time(row):
    time_1 ='2022-06-25 09:10:00'
    time_2 = '2022-06-25 12:00:00'
    time_3 = '2022-06-25 14:00:00'
    cnt = {}
    join_time = str(getattr(row,'join_time'))
    leaving_time = str(getattr(row,'leaving_time'))
    if (join_time > time_1) & (leaving_time <= time_2):
        cnt['morning_time_cnt'] = getTimeDiff(join_time, leaving_time, 2)
        cnt['afternoon_time_cnt'] = 0
        cnt['all_time_cnt'] = cnt['morning_time_cnt']
    if (join_time > time_2) & (leaving_time > time_3):
        cnt['morning_time_cnt'] = 0
        cnt['afternoon_time_cnt'] = getTimeDiff(join_time, leaving_time, 2)
        cnt['all_time_cnt'] = cnt['afternoon_time_cnt'] 
    if (join_time < time_2) & (leaving_time > time_3):
        cnt['morning_time_cnt'] = getTimeDiff(join_time, '2022-06-25 12:00:00', 2)
        cnt['afternoon_time_cnt'] = getTimeDiff(time_3, leaving_time, 2)
        cnt['all_time_cnt'] = cnt['morning_time_cnt'] + cnt['afternoon_time_cnt']
    return  cnt

x=[]
for row in df_info.itertuples():
    row1 = cnt_all_time(row)
    x.append(row1)

df = pd.DataFrame(x)
df_info = pd.concat([df_info, df], axis = 1)
df_info

你可能感兴趣的:(python,pandas)