You can do this with merge
:
df_merged = df1.merge(df2, how='outer', left_index=True, right_index=True)
The keyword argument how='outer'
keeps all indices from both frames, filling in missing indices with NaN
. The left_index
and right_index
keyword arguments have the merge be done on the indices. If you get all NaN
in a column after doing a merge, another troubleshooting step is to verify that your indices have the same dtypes
.
The merge
code above produces the following output for me:
V1 V2
A 2012-01-01 12.0 15.0
2012-02-01 14.0 NaN
2012-03-01 NaN 21.0
B 2012-01-01 15.0 24.0
2012-02-01 8.0 9.0
C 2012-01-01 17.0 NaN
2012-02-01 9.0 NaN
D 2012-01-01 NaN 7.0
2012-02-01 NaN 16.0