0

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:

  1. I want to filter the data - I only want from 9am to 4pm each day.
  2. 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
Caesar
  • 6,733
  • 4
  • 38
  • 44
  • 1
    can you include the data as well? We don't have access to your `app`. You also mention not being able to compare date and time objects which is possible: https://stackoverflow.com/a/8142411/12763382 – oh_my_lawdy Jan 11 '22 at 18:48
  • the 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 – Jamie Hall Jan 11 '22 at 23:08

0 Answers0