array to pandas dataframe

itsram90
itsram90 edited December 2016 in Python client
Hi,

i am trying to use historical data output as record. this output is in for of array.
to access previous candle data i am using pandas(suggest if anything is better).

import pandas as pd
def get_historical_data():
return kite.historical(instrument_token , from_dt , to_dt , interval)

array=records
df = pd.DataFrame(array)

i am getting error:
raise ValueError('If using all scalar values, you must pass'
ValueError: If using all scalar values, you must pass an index

i want to use index from records i.e. high low open close.
my main purpose if to fetch previous candle high low open and close.

Thanks & Regards
Ram
Tagged:
  • menaveenn
    Hi Ram,

    See if you can use the below function which I had written for my use:
    def capture_fn(his,inst):
    his=kite_Accnt1.historical(int(nse_inst['Instrument_Type'][Inst]),day1,day2,'minute')
    Close=[k['close'] for k in his]
    Open_1=[k['open'] for k in his]
    Low=[k['low'] for k in his]
    High=[k['high'] for k in his]
    date1=[k['date'] for k in his]
    Volume=[k['volume'] for k in his]
    df=pd.DataFrame([Close,Open_1,Low,High,date1,Volume]).transpose()
    df.columns=['Close','Open','Low','High','Date','Volume']
    df['Inst']=[inst for k in Close]
    #df['date_1']=[day for k in Close]
    return df
    Regards,
    Naveen
  • AnkitDoshi

    import pandas as pd
    def get_historical_data():
    return kite.historical(instrument_token , from_dt , to_dt , interval)
    No need to define function again.

    You can do this directly
    Data= kite.historical(instrument_token , from_dt , to_dt , interval)
    Data=pd.DataFrame(Data)

    For rearranging and setting index
    Data=Data[['date','open','high','low','close','volume']]
    Data=Data.set_index('date')
  • AnkitDoshi
    @menaveenn
    Hi,

    Close=[k['close'] for k in his]
    Open_1=[k['open'] for k in his]
    Low=[k['low'] for k in his]
    High=[k['high'] for k in his]
    date1=[k['date'] for k in his]
    Volume=[k['volume'] for k in his]
    df=pd.DataFrame([Close,Open_1,Low,High,date1,Volume]).transpose()
    df.columns=['Close','Open','Low','High','Date','Volume']



    what will this code do?
    Is it renaming column?
  • itsram90
    thanks @menaveenn i trying this. will post back results
  • menaveenn
    @AnkitDoshi Basically if you have more data points like open,close,low ,high and other details like volume etc ,in that case we can take whatever data points required and you can store only those in lists and later we can create a dataframe with them.

    I used read huge content from elasticsearch(in my office ) in that case some times my server goes out of memory due to unnecessary datapoints ,so we used run in batches and store only required columns/datapoints which are required.

    Here its not required ,but I'm still using it as a practice.

    Regards,
    Naveen
  • AnkitDoshi
    @menaveenn Ah!. You mean Series... Okiee cool
  • sambutle
    This error message suggests that a function or operation is expecting an index to be passed, but one was not provided. This can occur when attempting to access or manipulate elements of an array without specifying the index of the element to be accessed or manipulated. To resolve this error, ensure that the appropriate index or indices are specified when attempting to access or manipulate array elements. If working with scalar values, pass an index to indicate which value should be used.

Sign In or Register to comment.