user1934212 user1934212 - 2 months ago 17
Python Question

Select rows of DataFrame with datetime index based on date

From the following DataFrame with datetime index

'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.

Whats the best way to achieve that?

Answer

DatetimeIndex supports partial datetime strings for label based indexing:

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