0

How to convert the output into a dataframe? It is basically a dictionary. Tried _from dict.

Python code:

startdate = datetime.date(2017,1,12)
expirydate = datetime.date(2017,1,19)
data = dict()

for x in range(0,3):
    for y in range(1,8):
        startdate += datetime.timedelta(days=1)
        if ((startdate.weekday() !=5) and (startdate.weekday() !=6)):

            data [(x,y)] = get_history(symbol="BANKNIFTY",
                        start= startdate,
                        end= startdate,
                        index=True,
                        option_type='CE',
                        strike_price= int(df['CE Strike'][y]),
                        expiry_date=expirydate)

    expirydate += datetime.timedelta(days=7)

Output:

{(0,
  1):                Symbol      Expiry Option Type  Strike Price   Open  High  Low  \
 Date                                                                            
 2017-01-13  BANKNIFTY  2017-01-19          CE       19400.0  24.45  26.4  8.5   

Close   Last  Settle Price  Number of Contracts      Turnover  \

 Date                                                                        
 2017-01-13   13.6  14.45          13.6                35094  2.725216e+10   

 Premium Turnover  Open Interest  Change in OI  Underlying  

 Date                                                                   
 2017-01-13        19213000.0         168360         85160     18912.1  ,
 (0,
  4):                Symbol      Expiry Option Type  Strike Price  Open  High   Low  \
 Date                                                                            
 2017-01-16  BANKNIFTY  2017-01-19          CE       19400.0  11.9  32.0  7.05   

             Close  Last  Settle Price  Number of Contracts      Turnover  \
 Date                                                                       
 2017-01-16  25.75  28.3         25.75                81059  6.296796e+10   

             Premium Turnover  Open Interest  Change in OI  Underlying  
 Date                                                                   
 2017-01-16        66171000.0         254920         86560    19096.45  ,
 (0,
  5):                Symbol      Expiry Option Type  Strike Price  Open  High  \
 Date                                                                      
 2017-01-17  BANKNIFTY  2017-01-19          CE       19300.0  47.0  67.8   

               Low  Close  Last  Settle Price  Number of Contracts  \
 Date                                                                
 2017-01-17  21.95   31.8  25.1          31.8               168465   

                 Turnover  Premium Turnover  Open Interest  Change in OI  \
 Date                                                                      
 2017-01-17  1.303265e+11       271486000.0         525800        233600   

             Underlying  
 Date                    
 2017-01-17    19067.05  ,
 (0,
  6):                Symbol      Expiry Option Type  Strike Price   Open  High  \
 Date                                                                       
 2017-01-18  BANKNIFTY  2017-01-19          CE       19300.0  26.35  75.6   

               Low  Close  Last  Settle Price  Number of Contracts  \
 Date                                                                
 2017-01-18  17.45  28.45  29.0         28.45               271954   

                 Turnover  Premium Turnover  Open Interest  Change in OI  \
 Date                                                                      
 2017-01-18  2.104234e+11       474900000.0         816520        290720   

             Underlying  
 Date                    
 2017-01-18     19164.5  ,
 (0,
  7):                Symbol      Expiry Option Type  Strike Price  Open  High   Low  \
 Date                                                                            
 2017-01-19  BANKNIFTY  2017-01-19          CE       19300.0  26.0  26.0  0.05   

             Close  Last  Settle Price  Number of Contracts      Turnover  \
 Date                                                                       
 2017-01-19    0.1  0.05           0.0               586259  4.526496e+11   

             Premium Turnover  Open Interest  Change in OI  Underlying  
 Date                                                                   
 2017-01-19        57618000.0        1461560        645040    19124.25 
karel
  • 5,489
  • 46
  • 45
  • 50
  • How single row of you desired dataframe should look like? – Sharky Feb 23 '19 at 18:59
  • @Sharky Date Symbol Expiry Option Type Strike Price Open High Low Close Last Settle Price Number of Contracts Turnover Premium Turnover Open Interest Change in OI Underlying These are the requisite column headers. The rows would be different dates. – Puneet Tewani Feb 24 '19 at 07:43
  • As per documentation, `nsepy.get_history` returns dataframe. Could you please define your desired result clearer? – Sharky Feb 24 '19 at 10:11
  • @Sharky I'll elaborate. data [(x,y)] when I print it it yields only the last value stored in it and yes when I check its type it is a pandas df. But when I need to fetch all the values stored in data[(x,y)] till now. I have to give command as data, which it considers as a dict. When I try to convert data[(x,y)] to csv it does as it is considered as df but when I convert data to csv it gives an error as 'dict' object has no attribute 'to_csv' – Puneet Tewani Feb 25 '19 at 18:25
  • outfile = open( 'dict.csv', 'w' ) for key, value in sorted( data.items() ): outfile.write( str(key) + str(value)) Tried this code it worked. But the column headers are coming in different rows, same as shown above in the output. How can I format them in requisite format. – Puneet Tewani Feb 25 '19 at 18:59
  • @Sharky (0, 4) Symbol Expiry Option Type Strike Price Open High Low \ Date 2017-01-16 BANKNIFTY 2017-01-19 CE 19400.0 11.9 32.0 7.05 Close Last Settle Price Number of Contracts Turnover \ Date 2017-01-16 25.75 28.3 25.75 81059 6.296796e+10 Premium Turnover Open Interest Change in OI Underlying Date 2017-01-16 66171000.0 254920 86560 19096.45 This is one set of data with header – Puneet Tewani Feb 25 '19 at 19:13

0 Answers0