pd.concat() 未合并在同一索引上

Dom*_*aul 5 python dataframe python-3.x pandas

我有一个包含预测的 DataFrame,fcst如下所示:

             yhat        yhat_lower  yhat_upper
ds          
2015-08-31  -0.443522   -19.067399  17.801234
2015-09-30  6.794625    -31.472186  46.667981
...
Run Code Online (Sandbox Code Playgroud)

进行此转换后:

fcst2 = fcst["yhat"].to_frame().rename(columns={"yhat":"test1"})
fcst3 = fcst["yhat"].to_frame().rename(columns={"yhat":"test2"})
Run Code Online (Sandbox Code Playgroud)

我想将它们连接在日期索引上,如下所示:

pd.concat([fcst2,fcst3])
Run Code Online (Sandbox Code Playgroud)

但我收到一个未在索引上对齐的 DataFrame:

             test1     test2
ds      
2015-08-31  -0.443522   NaN
2015-09-30  6.794625    NaN
... ... ...
2017-05-31  NaN 95.563262
2017-06-30  NaN 85.829916
Run Code Online (Sandbox Code Playgroud)

尽管这样:

(fcst2.index == fcst3.index).any()
Run Code Online (Sandbox Code Playgroud)

返回 True。

我的问题是:为什么两个 DataFrame 没有在索引上连接起来,我能做些什么来解决这个问题?

我知道 join 函数,但由于我计划添加的其他一些 DataFrame 中会缺少一些日期,我相信该concat函数可能会更好。

use*_*881 3

调用设置concat为:axis1

pd.concat([df1, df2], axis=1)
Run Code Online (Sandbox Code Playgroud)