0

I'm collecting intraday 1-minute stock data from iexcloud using the API. I load the data into a pandas dataframe and then I am trying to grab a specific row of data for a specific day and minute during the day. I originally tried inputting in the original start variable (which is a datetime object) and that didn't work so I converted the date to pandas format using to_datetime() and tried printing it using print(df[start_p]) but this returns the same error as well. I tried searching older questions but to no avail since I feel the problem here likely stems from this additional "minute" component at the end. Below is the code and the picture shows the dataframe in question. If you look at the input and the corresponding dataframe picture you can see they're the exact same characters and I tried with both timestamp objects and string objects as the key but neither worked. How do I input the appropriate index into here?

import pandas as pd
import datetime #so don't have to do datetime.date())
from iexfinance.stocks import get_historical_data,get_historical_intraday
import matplotlib.pyplot as plt

api='xxx' #https://iexcloud.io/

start = datetime.datetime(2020, 11, 24)
end = datetime.datetime(2020, 12, 20)

df =get_historical_intraday("MSFT",start,output_format='pandas',token=api )
base='df'

for i in range(2): 


start += datetime.timedelta(days=1,hours=9,minutes=30)
start_p=pd.to_datetime(start)
#print(start_p)
#df=get_historical_intraday("MSFT",start,output_format='pandas',token=api)
att={'label':[215.23],'low':[213.92]} #for SO -- reproducing part of dataset
df=pd.DataFrame(att,index=[start_p])

print(df["2020-11-25 09:30:00"]) #KeyError: '2020-11-25 09:30:00'
print('here')
print(df[start_p]) #KeyError: Timestamp('2020-11-25 09:30:00')

Dataframe in question

Prospero
  • 109
  • 1
  • 11
  • Could you add the input data as text instead of a picture? – Shaido Dec 23 '20 at 05:54
  • @Shaido The data is coming through the API. I'm not using any files for uploading the data. – Prospero Dec 23 '20 at 06:46
  • 1
    Some testing data is needed to reproduce the problem but a picture can't be copied, see https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples on how to create a good reproducible example. – Shaido Dec 23 '20 at 07:03
  • @Shaido It's coming from the API though. I can add some data though. – Prospero Dec 23 '20 at 23:40
  • @Shaido It has been updated such that it can be reproduced. Thank you I understand what you meant. – Prospero Dec 23 '20 at 23:52
  • This should be able to help you: [How to access pandas DataFrame datetime index using strings](https://stackoverflow.com/questions/36871188/how-to-access-pandas-dataframe-datetime-index-using-strings). In short, try to use `df.loc["2020-11-25 09:30:00"]`. – Shaido Dec 24 '20 at 02:53

0 Answers0