猿问

如何在两个日期之间添加 date_range

我想处理一些日子之间的时间重叠。正如你在我的 df 中看到的,我的开始日期为 2019-10-25,结束日期为 2019-10-27:


begin                       end                          info

2019-10-25 10:39:58.352073  2019-10-25 10:40:06.266782   toto

2019-10-25 16:35:22.485574  2019-10-27 09:50:31.713179   tata <------ HERE

2019-10-27 09:50:31.713179  2019-10-27 09:50:31.713192   titi

2019-10-28 14:04:33.095633  2019-10-28 14:05:07.639344   tete

我想添加与这两个日期之间的时间段(日期 00:00:00;日期 23:59:59.9)一样多的时间段并复制数据info,如下所示:


2019-10-25 16:35:22.485574  2019-10-25 23:59:59.999999   tata

2019-10-26 00:00:00.000000  2019-10-26 23:59:59.999999   tata

2019-10-27 00:00:00.000000  2019-10-27 09:50:31.713179   tata

如果开始日期与结束日期不同,则 => 计算天数

保留开始并添加新的结束“日期 23:59:59.9”

添加新的 date_range 对应的天数

取结束并添加新的开始 'date 00:00:00.0'

填写“信息”

最终预期结果:


begin                       end                          info

2019-10-25 10:39:58.352073  2019-10-25 10:40:06.266782   toto


2019-10-25 16:35:22.485574  2019-10-25 23:59:59.999999   tata

2019-10-26 00:00:00.000000  2019-10-26 23:59:59.999999   tata

2019-10-27 00:00:00.000000  2019-10-27 09:50:31.713179   tata


2019-10-27 09:50:31.713179  2019-10-27 09:50:31.713192   titi

2019-10-28 14:04:33.095633  2019-10-28 14:05:07.639344   tete

但我不知道如何实现 date_range、填充信息、添加具体行数。


谢谢你的时间


慕田峪4524236
浏览 97回答 1
1回答

慕标5832272

假设begin和end已经是Timestamp类型:# Generate a series of Timedeltas for each rown = (&nbsp; &nbsp; (df['end'].dt.normalize() - df['begin'].dt.normalize())&nbsp; &nbsp; &nbsp; &nbsp; .apply(lambda d: [pd.Timedelta(days=i) for i in range(d.days+1)])&nbsp; &nbsp; &nbsp; &nbsp; .explode()).rename('n')df = df.join(n)# Adjust the begin and end of each rowadjusted_begin = np.max([&nbsp; &nbsp; df['begin'],&nbsp; &nbsp; df['begin'].dt.normalize() + df['n']], axis=0)adjusted_end = np.min([&nbsp; &nbsp; df['end'],&nbsp; &nbsp; pd.Series(adjusted_begin).dt.normalize() + pd.Timedelta(days=1, milliseconds=-100)], axis=0)# Final assemblydf = df.assign(begin_=adjusted_begin, end_=adjusted_end)结果:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; info&nbsp; &nbsp; &nbsp; n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin_&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end_0 2019-10-25 10:39:58.352073 2019-10-25 10:40:06.266782&nbsp; toto 0 days 2019-10-25 10:39:58.352073 2019-10-25 10:40:06.2667821 2019-10-25 16:35:22.485574 2019-10-27 09:50:31.713179&nbsp; tata 0 days 2019-10-25 16:35:22.485574 2019-10-25 23:59:59.9000001 2019-10-25 16:35:22.485574 2019-10-27 09:50:31.713179&nbsp; tata 1 days 2019-10-26 00:00:00.000000 2019-10-26 23:59:59.9000001 2019-10-25 16:35:22.485574 2019-10-27 09:50:31.713179&nbsp; tata 2 days 2019-10-27 00:00:00.000000 2019-10-27 09:50:31.7131792 2019-10-27 09:50:31.713179 2019-10-27 09:50:31.713192&nbsp; titi 0 days 2019-10-27 09:50:31.713179 2019-10-27 09:50:31.7131923 2019-10-28 14:04:33.095633 2019-10-28 14:05:07.639344&nbsp; tete 0 days 2019-10-28 14:04:33.095633 2019-10-28 14:05:07.639344剪掉不需要的列
随时随地看视频慕课网APP

相关分类

Python
我要回答