将时间序列数据拆分为每日粒度

时间:2019-08-14 01:36:17

标签: python pandas

我有带有开始和结束日期的事件数据,我正在寻找一种使用python将其拆分为每日粒度的简单方法。我目前试图在熊猫中使用数据框来实现它,但是我却一无所获。

数据位于CSV文件中,例如:

Event_Name,Dur,Planned_Start,Planned_Finish

Event 1,230.0h,29/06/2019 20:00,9/07/2019 10:00

Event 2,230.0h,29/06/2019 20:00,9/07/2019 10:00

Event 3,270.0h,29/06/2019 22:00,11/07/2019 4:00

Event 4,260.0h,29/06/2019 22:00,10/07/2019 18:00

Event 5,294.0h,29/06/2019 22:00,12/07/2019 4:00

Event 6,294.0h,29/06/2019 22:00,12/07/2019 4:00

Event 7,310.0h,2/07/2019 10:00,15/07/2019 8:00

Event 8,100.0h,2/07/2019 10:00,6/07/2019 14:00

我已经设法在excel中编写了一个宏来执行此操作,但是它非常慢,因为有1000多个事件

这是我在VB中编写的示例代码:

Dim i As Long
i = 8

Do
StartofDelay = Worksheets("Sheet1").Cells(i, 4)
EndofDelay = Worksheets("Sheet1").Cells(i, 5)
EOD = Worksheets("Sheet1").Cells(i, 2)


    If EOD > StartofEvent And EOD < EndofEvent Then

    Rows(i).Select
    Selection.Copy
    Rows(i + 1).Select
    Selection.Insert Shift:=xlDown
    Worksheets("Sheet1").Cells(i, 5) = EOD
    Worksheets("Sheet1").Cells(i + 1, 4) = EOD

    Else
    'Do Nothing

    End If

i = i + 1
Loop Until StartofEvent = ""

我要达到的目标的示例:如果事件在1月1日下午6点开始,在1月3日下午2点结束,那么它应该变成3个事件:

-Event 1: 1800 January 1st to 2359 January 1st

-Event 1: 2400 January 2nd to 2359 January 2nd

-Event 1: 2400 January 3rd to 1400 January 3rd

0 个答案:

没有答案