<p>假设我们有以下DFs:</p>
<pre><code>In [44]: df1
Out[44]:
1996Q2 2000Q3 2010Q4
0 1.5 3.5 1.000000
1 22.0 38.5 2.000000
2 15.0 35.0 4.333333
In [45]: df1.columns
Out[45]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
</code></pre>
<p>注意:<code>df1.columns</code>属于<code>PeriodIndex</code>数据类型</p>
^{pr2}$
<p><code>merge</code>和<code>join</code>将返回:<code>ValueError: can only call with other PeriodIndex-ed objects</code>因为,AFAIK,Pandas DF不能有混合列数据类型,如果其中一些是<code>PeriodIndex</code>数据类型:</p>
<pre><code>In [48]: df1.join(df2)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects
</code></pre>
<p><code>merge</code>引发相同的异常:</p>
<pre><code>In [54]: pd.merge(df1, df2, left_index=True, right_index=True)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects
</code></pre>
<p>因此我们必须将<code>df1.columns</code>转换为字符串:</p>
<pre><code>In [49]: df1.columns = df1.columns.values.astype(str)
In [50]: df1.columns
Out[50]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')
</code></pre>
<p>现在<code>join</code>和{<cd3>}将工作:</p>
<pre><code>In [51]: df1.join(df2)
Out[51]:
1996Q2 2000Q3 2010Q4 a b c
0 1.5 3.5 1.000000 a1 b1 c1
1 22.0 38.5 2.000000 a2 b2 c2
2 15.0 35.0 4.333333 a3 b3 c3
In [52]: pd.merge(df1, df2, left_index=True, right_index=True)
Out[52]:
1996Q2 2000Q3 2010Q4 a b c
0 1.5 3.5 1.000000 a1 b1 c1
1 22.0 38.5 2.000000 a2 b2 c2
2 15.0 35.0 4.333333 a3 b3 c3
</code></pre>
<p>合并数据框的<code>dtypes</code>列:</p>
<pre><code>In [58]: df1.join(df2).columns
Out[58]: Index(['1996Q2', '2000Q3', '2010Q4', 'a', 'b', 'c'], dtype='object')
</code></pre>
<p>如果合并完成后需要<code>df1.columns</code>作为<code>PeriodIndex</code>,可以在转换它们之前保存{<cd1>},并在完成合并/合并后将其设置回:</p>
<pre><code>In [60]: df1.columns
Out[60]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
In [61]: cols_saved = df1.columns
In [62]: df1.columns = df1.columns.values.astype(str)
In [63]: df1.columns
Out[63]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')
# merging (joining) or doing smth else here ...
In [64]: df1.columns = cols_saved
In [65]: df1.columns
Out[65]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
</code></pre>