E.TTT
E.TTT

Reputation: 11

How to remove a certain time period from a dataframe with timestamp index?

I have a DataFrame that is something like this:

Datetime             Open   Low/Open  Close/Open  Tar

1998-01-30 15:30:00  68.02  0.997501    0.999706    0
1998-01-30 15:45:00  67.91  0.998086    1.000736    0
1998-01-30 16:00:00  67.96  0.998676    1.000589    0
1998-01-30 16:15:00  67.96  1.000000    1.000000    0

the timestamp is 24 hours, however, I only want the data points that have index between 9:30:00 to 16:00:00. How do I remove all the data points outside of what I need?

Upvotes: 0

Views: 1218

Answers (1)

chrisaycock
chrisaycock

Reputation: 37930

The .dt suffix of any datetime column has array-friendly routines.

import datetime

df[(df.Datetime.dt.time >= datetime.time(9, 30)) &
   (df.Datetime.dt.time <= datetime.time(16, 0))]

From your sample, I get:

             Datetime   Open  Low/Open  Close/Open  Tar
0 1998-01-30 15:30:00  68.02  0.997501    0.999706    0
1 1998-01-30 15:45:00  67.91  0.998086    1.000736    0
2 1998-01-30 16:00:00  67.96  0.998676    1.000589    0

Upvotes: 1

Related Questions