pandas 根据日期选择具有日期时间索引的 DataFrame 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39961862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:10:32  来源:igfitidea点击:

Select rows of DataFrame with datetime index based on date

pythondatetimepandas

提问by user1934212

From the following DataFrame with datetime index

从以下带有日期时间索引的 DataFrame

                                       'A'
2015-02-17 14:31:00+00:00           127.2801
2015-02-17 14:32:00+00:00           127.7250
2015-02-17 14:33:00+00:00           127.8010
2015-02-17 14:34:00+00:00           127.5450
2015-02-17 14:35:00+00:00           127.6300
...
2016-02-17 20:56:00+00:00            98.0900
2016-02-17 20:57:00+00:00            98.0901
2016-02-17 20:58:00+00:00            98.1000
2016-02-17 20:59:00+00:00            98.0500
2016-02-17 21:00:00+00:00            98.1100

I want to select all rows with a certain date, e.g. 2015-02-17.

我想选择具有特定日期的所有行,例如 2015-02-17。

Whats the best way to achieve that?

实现这一目标的最佳方法是什么?

采纳答案by EdChum

DatetimeIndexsupports partial datetime strings for label based indexing:

DatetimeIndex支持基于标签索引的部分日期时间字符串:

In [18]:
df.loc['2015-02-17']

Out[18]:
                            A
2015-02-17 14:31:00  127.2801
2015-02-17 14:32:00  127.7250
2015-02-17 14:33:00  127.8010
2015-02-17 14:34:00  127.5450
2015-02-17 14:35:00  127.6300