Index._join_level on non-unique index is not implemented--Resolved

Hi vibhu,



Pls support joining 2 datasets. Pls, find the Code details.



from nsepy import get_history

import datetime

from datetime import date

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np



s= date(2020,12,31)



nifty_fut = get_history(symbol="NIFTY",

        start=s,

        end=s,

        index=True,

        futures=True,

        expiry_date=s)

niftydata= nifty_fut



nifty_opt=get_history(symbol="NIFTY",

        start=s,

        end=s,

        index=True,

        option_type='CE',

        strike_price=15000,

        expiry_date=s)



optiondata = nifty_opt



s= s + datetime.timedelta(days=1)



expiry= [date (2021,1,28), date (2021,2,25),date (2021,3,25)]



for x in expiry:

    expiry=x

    nifty_fut= get_history(symbol="NIFTY",

                          start=s,

                          end=x,

                          index=True,

                          futures=True,

                          expiry_date=x)

    

    niftydata= niftydata.append(nifty_fut)

    

    high= nifty_fut[['Close']].max()

    low= nifty_fut[['Close']].min()

    high= int((round(high/100)*100)+100)

    low= int((round(low/100)*100)-100)

    

    

    for z in range (low, high, 100):

        nifty_opt= get_history(symbol="NIFTY",

                              start=s,

                              end=expiry,

                              index=True,

                              option_type='CE',

                              strike_price=z,

                              expiry_date=x)

        optiondata= optiondata.append(nifty_opt)

        nifty_opt= get_history(symbol="NIFTY",

                              start=s,

                              end=expiry,

                              index=True,

                              option_type='PE',

                              strike_price=z,

                              expiry_date=x)

        optiondata= optiondata.append(nifty_opt)

        

s= x + datetime.timedelta(days=1)

######

NiftyCF = pd.DataFrame({"Nifty": niftydata["Close"]})

NiftyCO= pd.DataFrame ({"Expiry": optiondata['Expiry'],

                       "Type":optiondata['Option Type'],

                       "Strike":optiondata['Strike Price'],

                       "Last":optiondata['Last']})

Opttable = pd.pivot_table(NiftyCO, values= 'Last', index =['Date', 'Type','Expiry'],

                         columns= ['Strike'], aggfunc= np.sum)





#########(Error after using .join)





Opttable=Opttable.join(NiftyCF)

3760                 "Index._join_level on non-unique index " "is not implemented"
   3761             )
   3762 

NotImplementedError: Index._join_level on non-unique index is not implemented

Hi Mukul,



The way you are appending the three dataframes, that seems an incorrect way to join Futures contract.

An example of duplicate data can be seen here:





The blue part is the first contract, followed by the black part from the second contract. As you can see here, the dataframe which got appended there duplicates the value for january.



This is causing problems when you are joining. The solution is to either use appropriate from and to dates, for the three dataframes you fetch and append. Or, to use a different method to get the continuous futures data where you select the first month contract and roll the the contracts on expiry.



Hope this helps!

Gaurav

Thanks Gaurav…Here is the updated code.





######



NiftyCF = pd.DataFrame({"Nifty": niftydata["Close"]})

NiftyCO= pd.DataFrame ({"Expiry": optiondata['Expiry'],

                       "Type":optiondata['Option Type'],

                       "Strike":optiondata['Strike Price'],

                       "Last":optiondata['Last']})





NiftyCO=NiftyCO.join(NiftyCF)



Opttable = pd.pivot_table(NiftyCO, values= 'Last', index =['Date','Nifty','Type','Expiry'],columns= ['Strike'], aggfunc= np.sum)

Hi Mukul,



Glad that this issue got resolved.



Thanks!