我有一个解决的问题更新列值基于长度临时数据帧基于源数据帧
有一个代码:
import pandas as pd
df = pd.DataFrame({
'ID': ['aaa', 'aaa', 'aaa', 'bbb', 'ccc', 'ccc'],
'closed': ['2023-03-28 22:00', '2023-03-28 22:00', '2023-03-28 22:00', '2023-03-29 23:00', '2023-03-27 22:00', '2023-03-27 22:00'],
'set': ['2023-03-27 19:00', '2023-03-28 19:30', '2023-03-28 20:00', '2023-03-27 22:00', '2023-03-25 19:00', '2023-03-26 19:30'],
'message_time': ['19:05', '19:40', '21:00', '22:10', '19:05', '19:40']
})
df['newtime'] = (s.groupby(df['ID']).diff(-1).mul(-1)
.fillna(c-s)
.dt.total_seconds().div(60)
)
输出为分差(fillna(c-s)
)。我得把这个换成商务会议记录。我试过了,但没有用。例如,只需使用模块business_duration:
import holidays as pyholidays
from datetime import time, datetime
holidaylist_RU = pyholidays.Russia(years=[datetime.now().year, datetime.now().year-1])
start_hour = time(10, 0, 0)
end_hour = time(21, 0, 0)
unit_min='min'
DATA_REACTION['reaction (minutes)'] = DATA_REACTION.apply(lambda x: bd.businessDuration(datetime.strptime(x['start'], '%Y-%m-%d %H:%M:%S'), datetime.strptime(x['end'], '%Y-%m-%d %H:%M:%S'), start_hour, end_hour, holidaylist=holidaylist_RU, unit=unit_min), axis=1)
适用于直接应用。如何实现这个解决方案的fillna
行?
import numpy as np
import pandas as pd
import business_duration as bd
import holidays as pyholidays
from datetime import time, datetime
holidaylist_RU = pyholidays.Russia(years=[datetime.now().year, datetime.now().year-1])
start_hour = time(10, 0, 0)
end_hour = time(21, 0, 0)
unit_min='min'
df = pd.DataFrame({
'ID': ['aaa', 'aaa', 'aaa', 'bbb', 'ccc', 'ccc'],
'closed': ['2023-03-28 22:00', '2023-03-28 22:00', '2023-03-28 22:00', '2023-03-29 23:00', '2023-03-27 22:00', '2023-03-27 22:00'],
'set': ['2023-03-27 19:00', '2023-03-28 19:15', '2023-03-28 20:00', '2023-03-27 22:00', '2023-03-25 19:00', '2023-03-26 19:30'],
'message_time': ['19:05', '19:40', '21:00', '22:10', '19:05', '19:40']
})
df['set'] = pd.to_datetime(df['set'])
df['closed'] = pd.to_datetime(df['closed'])
df = df.sort_values(['ID', 'set'])
df['newtime_1'] = df.groupby('ID')['set'].shift(-1).fillna(df['closed'])
df['in work (minutes)'] = df.apply(lambda x: bd.businessDuration(x['set'], x['newtime_1'], start_hour, end_hour, holidaylist=holidaylist_RU, unit=unit_min), axis=1)
找到解决方案了!