0

Trying to convert 1 min ohlc into 1 day stock data.

I have two dataframes

  1. df - 1min dataframe
  2. df2- 1 day dataframe which I'm trying to make

df has the following columns

cols = ["name", "date","time", "open", "high","low","close","sm","volume"]

I'm initialising df2 like this

df2 = pd.DataFrame( columns = ["name", "date", "open", "high","low","close"])

Sample data of df

name            date        time    open    high    low     close   sm      volume
BANKNIFTY_F1    20190101    09:16   27300.2 27368.1 27300.2 27314.9 18300   1414860
BANKNIFTY_F1    20190101    09:17   27317.7 27322.1 27289.9 27299.3 11980   1414860
BANKNIFTY_F1    20190101    09:18   27298.6 27309.0 27296.2 27307.0 10760   1414860
BANKNIFTY_F1    20190101    09:19   27302.7 27308.7 27277.0 27282.0 13620   1424420
BANKNIFTY_F1    20190101    09:20   27282.0 27282.0 27257.5 27264.4 16040   1424420

Like this df has 1 min data for an entire month.

And I want to insert 1 day from df into df2.

For starters I tried to insert the open and close price of the day first into df2 like this

for j in range(df['date'].nunique()):
    #open and close
    for i in range(len(df)):
        #open
        if(df['time'][i]=='09:16'):
            df2['open'][j]=df['open'][0]
        #close    
        if(df['time'][i]=='15:30'):
            df2['close'][j]=df['close'][i]

This is returning me with error

File "<ipython-input-26-43c7b22fee98>", line 1, in <module>
    df2['open'][0]=df['open'][0]

  File "/home/devang/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 1020, in __setitem__
    values[key] = value

IndexError: index 0 is out of bounds for axis 0 with size 0

What should I do?

Also how can I can get the lowest and highest price of each day considering df has 1min data for an entire month in it?

4

1 回答 1

0

您可以分组date,然后聚合相关列。使用您的示例数据,它可能是:

df.groupby('date').agg({'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'})

给予:

             open     high      low    close
date                                        
20190101  27300.2  27368.1  27257.5  27264.4
于 2020-07-20T14:01:42.040 回答