1

I have a Pandas datafame indexed by datetime and I would like to select indexes between 2 dates: start and end.

type(start)
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

type(end)
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

EDIT

Reproducible code:

from datetime import timedelta
idx = pd.DatetimeIndex(['2021-10-16 15:00:00', '2021-10-16 16:00:00',
           '2021-10-16 17:00:00', '2021-10-16 18:00:00',
           '2021-10-16 19:00:00', '2021-10-16 20:00:00',
           '2021-10-16 21:00:00', '2021-10-16 22:00:00',
           '2021-10-16 23:00:00', '2021-10-17 00:00:00',
           '2021-10-17 01:00:00', '2021-10-17 02:00:00',
           '2021-10-17 03:00:00', '2021-10-17 04:00:00',
           '2021-10-17 05:00:00', '2021-10-17 06:00:00',
           '2021-10-17 07:00:00', '2021-10-17 08:00:00',
           '2021-10-17 09:00:00', '2021-10-17 10:00:00',
           '2021-10-17 11:00:00', '2021-10-17 12:00:00',
           '2021-10-17 13:00:00', '2021-10-17 14:00:00',
           '2021-10-17 15:00:00', '2021-10-17 16:00:00'],
          dtype='datetime64[ns]', freq='H')
df = pd.DataFrame([range(25)], index=idx)
end = df.tail(1).index
start = end - timedelta(hours=5)
df.loc[(df.index >= start) & (df.index <= end)]

ValueError: Lengths must match

How can I do that ?

4
  • Does this answer your question? Select DataFrame rows between two dates Commented Oct 17, 2021 at 15:18
  • Unfortunatly not, because when I try prices.index > start it throws an error saying ValueError: Lengths must match Commented Oct 17, 2021 at 15:19
  • Do I have to convert DatetimeIndex into a string ? Commented Oct 17, 2021 at 15:21
  • if your search date is a string, then you could convert the column to string. But for obvious reasons, it's quicker to change the searchable object into a datetime object. Commented Oct 17, 2021 at 15:27

2 Answers 2

2

Your start and end variables need to be datetimes, not datetime indexes.

Try end = df.index[-1]

Sign up to request clarification or add additional context in comments.

Comments

2

You can use an ordinary query, but your dates need to be datetime objects.

dt1 = datetime.datetime(some date)
dt2 ...

rng = df[(df.index >= dt1 ) & (df.index <= dt2)]

This will give you the frame of the desired range of dates.

See Fergus answer, you were searching with an index and not a date.

3 Comments

When I try prices.index > start it throws an error saying ValueError: Lengths must match
@Florent, you need to post both the date you are searching and the method, please post some reproducible code.
I eddited the question with reproducible code. Thanks

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.