简体   繁体   English

如何按日期列表过滤日期和小时的数据框?

[英]How to filter a dataframe of dates and hours by a list of dates?

I have the following dataframe, with several days and a measure every 15minutes:我有以下数据框,有几天,每 15 分钟测量一次:

                 CONSUMPTION
2016-11-01 01:00:00  3539
2016-11-01 01:15:00  3560
2016-11-01 01:30:00  3505
....
2016-11-02 01:00:00  3400
2016-11-02 01:15:00  3447
....
2016-11-03 01:00:00  2400
2016-11-03 01:15:00  2447

Every works if ask for a simple day:如果要求简单的一天,每一个都有效:

df['2016-11-01']

and I get all the measure for that day, 96 measures:我得到了那天的所有测量值,96 个测量值:

    CONSUMPTION
2016-11-01 01:00:00  3539
2016-11-01 01:15:00  3560
2016-11-01 01:30:00  3505
....

But my problem is that I want to get a list of dates, and this doesn't work:我的问题是我想获取日期列表,但这不起作用:

df[['2016-11-01','2016-11-03']]

The most I have achieved is this (but this is not what I want, observed that 1 get a measure per day, instead my 96 expected measures):我取得的最大成就是这个(但这不是我想要的,观察到每天有 1 个度量,而不是我的 96 个预期度量):

qwe=['2016-11-01','2016-11-03']
df.ix[df.index.to_datetime().isin(qwe)]

            CONSUMPTION
2016-11-01  3539
2016-11-03  2400

Any idea would be appreciated.....任何想法将不胜感激......

Floor the datetime to Day and then use isin and loc ie 将日期时间置于日期,然后使用isin和loc ie

li = ['2016-11-01','2016-11-02']
df.loc[(df.index.floor('D').isin(li)),:]

                    CONSUMPTION
Date                            
2016-11-01 01:00:00         3539
2016-11-01 01:15:00         3560
2016-11-01 01:30:00         3505
2016-11-02 01:00:00         3400
2016-11-02 01:15:00         3447

This works for hours :这工作数小时:

hours = [0, 6, 12, 18]
df.loc[(df.index.hour.isin(hours)),:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM