Reputation: 3299
I have two df. The first df is a multiindex and the other one is typical single index.
Figure 1: Multiindex df
and
Figure 2: Single indexing
Upon join these two df, I got the following error
cannot join with no overlapping index names
I suspect, this error due to the index column name in the first df (Figure 1).
Even, swaping the index name and typical numeric value also does not help
Figure 2: Multiindex df
May I know how to address this error?
Thanks in advance for the time taken
Upvotes: 3
Views: 25547
Reputation: 31
If you are trying to do a function such as df.rolling(window).cov()/df.rolling(window).var() where you are trying to basically merge two multi-index dataframes what happened to me was I had to specify a name to the index as it doesn't know they name of the index to match on which is why you are getting this error. If you are using something like yfin to get data you won't run into this issue because the index always defaults as 'Date'. Here is a simple one-liner to fix this:
df.index.rename('Date', inplace=True)
Upvotes: 1
Reputation: 862581
You can convert first level in MultiIndex
to column before merge
:
df = (df1.reset_index(level=0)
.merge(df2, left_index=True, right_index=True)
.set_index('name', append=True)
.swaplevel(1, 0))
Or if use join:
df = df1.reset_index(level=0).join(df2).set_index('name', append=True).swaplevel(1, 0)
Upvotes: 7