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 ?

Florent
  • 1,791
  • 22
  • 40
  • Does this answer your question? [Select DataFrame rows between two dates](https://stackoverflow.com/questions/29370057/select-dataframe-rows-between-two-dates) – ajsp Oct 17 '21 at 15:18
  • Unfortunatly not, because when I try `prices.index > start` it throws an error saying `ValueError: Lengths must match` – Florent Oct 17 '21 at 15:19
  • Do I have to convert DatetimeIndex into a string ? – Florent Oct 17 '21 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. – ajsp Oct 17 '21 at 15:27

2 Answers2

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.

ajsp
  • 2,512
  • 22
  • 34
2

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

Try end = df.index[-1]