5

I have a pandas DataFrame with a DateTimeIndex:

                           A          B
2016-04-25 18:50:06   440.967796   201.049600  
2016-04-25 18:50:13   441.054995   200.767034  
2016-04-25 18:50:20   441.142337   200.484475
...
2016-07-27 18:50:06   440.967796   201.049600  
2016-07-27 18:50:13   441.054995   200.767034  
2016-07-27 18:50:20   441.142337   200.484475

I would like to extract all the data of a given date yyyy-mm-dd using a list of dates: ['2016-04-25','2016-04-28',...]

I tried the following:

 df[df.index.isin(['2016-04-25', '2016-04-26'])]

 Empty DataFrame

I would like to retrieve all the data (data of the whole day) of the dates given in this list

0

1 Answer 1

9

You need remove times first by this solutions:

df = df[df.index.normalize().isin(['2016-04-25', '2016-04-26'])]

df = df[df.index.floor('D').isin(['2016-04-25', '2016-04-26'])]

Another solution is compare DatetimeIndex.date, but necessary use numpy.in1d instead isin:

df = df[np.in1d(df.index.date, pd.to_datetime(['2016-04-25', '2016-04-26']).date)]

Or compare strings created DatetimeIndex.strftime:

df = df[np.in1d(df.index.strftime('%Y-%m-%d'), ['2016-04-25', '2016-04-26'])]

print (df)
                              A           B
2016-04-25 18:50:06  440.967796  201.049600
2016-04-25 18:50:13  441.054995  200.767034
2016-04-25 18:50:20  441.142337  200.484475
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I have a follow up question. Is it possible to cut off the first n data rows in each individual day, without copying any data? The days have different start/end times and different numbers of data rows
Hmmm, try df = df.drop(df.groupby(df.index.date).head(2).index) - it remove first 2 value from each date.

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.