Home > Net >  How to select index by datetime range with Pandas
How to select index by datetime range with Pandas

Time:10-18

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 ?

CodePudding user response:

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

Try end = df.index[-1]

CodePudding user response:

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.

  • Related