明智地过滤Dataframe日期和时间

时间:2017-03-17 12:06:25

标签: python pandas datetime dataframe

我有一个像这样的数据帧:

Timestamp   Maximum Demand  Consumption
3/1/2017 6:00   116 116
3/1/2017 7:00   1532    118
3/1/2017 8:00   2008    119
3/1/2017 9:00   3008    125
3/1/2017 10:00  4472    122
3/1/2017 11:00  4964    123
3/1/2017 12:00  5908    118
3/1/2017 13:00  7324    118
3/1/2017 14:00  7800    119
3/1/2017 15:00  8752    119
3/1/2017 16:00  10216   122
3/1/2017 17:00  10716   125
3/1/2017 18:00  11676   120
3/1/2017 19:00  13164   124
3/1/2017 20:00  13628   116
3/1/2017 21:00  14572   118
3/1/2017 22:00  16048   123
3/1/2017 23:00  16540   123
4/1/2017 0:00   17476   117
4/1/2017 1:00   18916   120
4/1/2017 2:00   19384   117
4/1/2017 3:00   20312   116
4/1/2017 4:00   21764   121
4/1/2017 5:00   22228   116
4/1/2017 6:00   23204   122
4/1/2017 7:00   24704   125
4/1/2017 8:00   25204   125
4/1/2017 9:00   26172   121
4/1/2017 10:00  27552   115
4/1/2017 11:00  28028   119
4/1/2017 12:00  28988   120
4/1/2017 13:00  30488   125
4/1/2017 14:00  30960   118
4/1/2017 15:00  31920   120
4/1/2017 16:00  33408   124
4/1/2017 17:00  33904   124
4/1/2017 18:00  34880   122

Date format: dd/mm/yyyy hh:mm

我正在尝试根据时间和日期过滤数据。

示例:从时间(07:00 - 18:00)(19:00 - 22:00)(23:00 - 06:00)开始 预期产出:

3/1/2017 7:00   1532    118
3/1/2017 8:00   2008    119
3/1/2017 9:00   3008    125
3/1/2017 10:00  4472    122
3/1/2017 11:00  4964    123
3/1/2017 12:00  5908    118
3/1/2017 13:00  7324    118
3/1/2017 14:00  7800    119
3/1/2017 15:00  8752    119
3/1/2017 16:00  10216   122
3/1/2017 17:00  10716   125
3/1/2017 18:00  11676   120

我是初学者,我很困惑。我怎样才能做到这一点? 请帮忙。

我在我的代码中尝试了这个:

offpeak = df2[(df2.index.hour == 6) & (df2.index.minute == 0)]
normal = df2[(df2.index.hour == 18) & (df2.index.minute == 0)]
peak = df2[(df2.index.hour == 22) & (df2.index.minute == 0)]

1 个答案:

答案 0 :(得分:2)

我认为您需要between_time,但首先转换列Timestamp to_datetime,然后从中设置索引:

df.Timestamp = pd.to_datetime(df.Timestamp, dayfirst=True)

df = df.set_index('Timestamp')
offpeak  = df.between_time('7:00', '18:00')
normal = df.between_time('19:00', '22:00')
peak = df.between_time('23:00', '6:00')
print (offpeak)
                     Maximum Demand  Consumption
Timestamp                                       
2017-01-03 07:00:00            1532          118
2017-01-03 08:00:00            2008          119
2017-01-03 09:00:00            3008          125
2017-01-03 10:00:00            4472          122
2017-01-03 11:00:00            4964          123
2017-01-03 12:00:00            5908          118
2017-01-03 13:00:00            7324          118
2017-01-03 14:00:00            7800          119
2017-01-03 15:00:00            8752          119
2017-01-03 16:00:00           10216          122
2017-01-03 17:00:00           10716          125
2017-01-03 18:00:00           11676          120
2017-01-04 07:00:00           24704          125
2017-01-04 08:00:00           25204          125
2017-01-04 09:00:00           26172          121
2017-01-04 10:00:00           27552          115
2017-01-04 11:00:00           28028          119
2017-01-04 12:00:00           28988          120
2017-01-04 13:00:00           30488          125
2017-01-04 14:00:00           30960          118
2017-01-04 15:00:00           31920          120
2017-01-04 16:00:00           33408          124
2017-01-04 17:00:00           33904          124
2017-01-04 18:00:00           34880          122

如果还需要按date过滤:

df.Timestamp = pd.to_datetime(df.Timestamp, dayfirst=True)
df = df.set_index('Timestamp')
offpeak  = df['2017-01-04'].between_time('7:00', '18:00')
print (offpeak)
                     Maximum Demand  Consumption
Timestamp                                       
2017-01-04 07:00:00           24704          125
2017-01-04 08:00:00           25204          125
2017-01-04 09:00:00           26172          121
2017-01-04 10:00:00           27552          115
2017-01-04 11:00:00           28028          119
2017-01-04 12:00:00           28988          120
2017-01-04 13:00:00           30488          125
2017-01-04 14:00:00           30960          118
2017-01-04 15:00:00           31920          120
2017-01-04 16:00:00           33408          124
2017-01-04 17:00:00           33904          124
2017-01-04 18:00:00           34880          122