选择两个DatetimeIndex日期之间的行 [英] Select rows between two DatetimeIndex dates
本文介绍了选择两个DatetimeIndex日期之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的CSV文件:
I have a CSV file of the following format:
vm,time,LoadInt1
abc-webapp-02,2017-05-31 10:00:00,3.133333
abc-webapp-02,2017-05-31 10:05:00,0.000000
abc-webapp-02,2017-05-31 10:10:00,0.000000
abc-webapp-02,2017-05-31 10:15:00,0.000000
abc-webapp-02,2017-05-31 10:20:00,0.000000
abc-webapp-02,2017-05-31 10:25:00,0.000000
abc-webapp-02,2017-05-31 10:30:00,0.000000
abc-webapp-02,2017-05-31 10:35:00,0.000000
abc-webapp-02,2017-05-31 10:40:00,0.000000
我使用以下代码将CSV文件读取到DataFrame中.日期被解析为索引(DatetimeIndex)
I read the CSV file into a DataFrame using the following code. The date is parsed as index (DatetimeIndex)
dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv("my_file.csv", header=0, parse_dates=[1], index_col=1, date_parser=dateparse)
现在,我正在尝试使用以下代码获取两个日期之间的所有行(真正的CSV文件在下面提到的日期之间具有大量的行):
Now I am trying to get all the rows between two dates using the following code (The real CSV file has large number of rows between the dates mentioned below):
df.loc['2017-05-30' : '2017-05-31']
请注意,建议在此处使用上述方法.但是,它对我不起作用.因此,这可能不是一个重复的问题.
Please note, above approach is suggested here. But, it's not working for me. So, it may not be a duplicate question.
推荐答案
使用query
方法:
df = pd.read_csv("my_file.csv", index_col=1, parse_dates=True)
In [121]: df.query("'2017-05-30' <= index <= '2017-06-01'")
Out[121]:
vm LoadInt1
time
2017-05-31 10:00:00 abc-webapp-02 3.133333
2017-05-31 10:05:00 abc-webapp-02 0.000000
2017-05-31 10:10:00 abc-webapp-02 0.000000
2017-05-31 10:15:00 abc-webapp-02 0.000000
2017-05-31 10:20:00 abc-webapp-02 0.000000
2017-05-31 10:25:00 abc-webapp-02 0.000000
2017-05-31 10:30:00 abc-webapp-02 0.000000
2017-05-31 10:35:00 abc-webapp-02 0.000000
2017-05-31 10:40:00 abc-webapp-02 0.000000
这篇关于选择两个DatetimeIndex日期之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文