1

the df looks like this:

DateTime
2017-07-10 03:00:00    288.0
2017-07-10 04:00:00    306.0
2017-08-10 05:00:00    393.0
2017-08-10 06:00:00    522.0
2017-09-10 07:00:00    487.0
2017-09-10 08:00:00    523.0
2017-10-10 09:00:00    585.0

Question how to select row that in a list of dates:

['2017-07-10', '2017-09-10']

to have:

DateTime
2017-07-10 03:00:00    288.0
2017-07-10 04:00:00    306.0
2017-09-10 07:00:00    487.0
2017-09-10 08:00:00    523.0

Thanks

2 Answers 2

6

Given that the dates in your list contain up to the daily information, you could start by flooring (Series.dt.floor) the DatetimeIndex up to the daily level and indexing with the list of datetime objects using isin:

t = [pd.to_datetime('2017-07-10'), pd.to_datetime('2017-09-10')]
df.index= pd.to_datetime(df.index)

df[df.index.floor('d').isin(t)]

Output

   DateTime
2017-07-10 03:00:00     288.0
2017-07-10 04:00:00     306.0
2017-09-10 07:00:00     487.0
2017-09-10 08:00:00     523.0
Sign up to request clarification or add additional context in comments.

1 Comment

for example? we can still use the date accesor right?
3

Assuming the Datetime is index, try with the below:

to_search=['2017-07-10', '2017-09-10']
df[df.index.to_series().dt.date.astype(str).isin(to_search)]

                        1
DateTime                  
2017-07-10 03:00:00  288.0
2017-07-10 04:00:00  306.0
2017-09-10 07:00:00  487.0
2017-09-10 08:00:00  523.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.