I am pulling data from Interactive Brokers' API and creating a dataframe. That part works well, but I have spent several days, and countless googling to get what I want, which is two things:
- I want to filter the data - I only want from 9am to 4pm each day.
- To set a flag when the time is 9:30. A helper column is fine, in fact easier for testing. Set the value of the row to 0 when it is NOT 9:30 and to 1 when it is. But I can't do any comparing because my dates and times are objects and not strings.
Any advice would be greatly appreciated. I am posting what I think is the pertinent code, but I can post more if needed.
import pandas as pd
df = pd.DataFrame(app.data, columns=['ReqID', 'Date', 'Open', 'High', 'Low','Close'])
df.drop(['ReqID'], axis=1, inplace=True)
df['just_date']=pd.to_datetime(df['Date'])
df['dates'] = df['just_date'].dt.date
df['times'] = df['just_date'].dt.time
df.drop(['Date', 'just_date'], axis=1, inplace=True)
df=df[['dates', 'times', 'Open', 'High', 'Low', 'Close']]
df.head()
df.head()
gives this:
dates times Open High Low Close
0 2022-01-06 18:00:00 15801.75 15814.25 15799.00 15811.50
1 2022-01-06 18:01:00 15811.50 15814.25 15804.50 15804.75
2 2022-01-06 18:02:00 15805.25 15808.25 15805.25 15806.50
3 2022-01-06 18:03:00 15806.75 15807.50 15804.25 15806.75
4 2022-01-06 18:04:00 15806.00 15807.00 15803.25 15804.50