如何将两个具有不同类型索引的数据框组合在一起(一个是DatetimeIndex,另一个是PeriodIndex)?

时间:2019-02-28 12:27:54

标签: python pandas

当pd.concat两个具有不同索引类型的df(一个是DatetimeIndex,另一个是PeriodIndex)时,我遇到了一个错误。

df1.index:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'...],
              dtype='datetime64[ns]', length=2022, freq=None)

df2.index:

PeriodIndex(['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',
             '2010-01-07'...],
            dtype='period[B]', name='Date', length=2304, freq='B')

错误消息:

 'Index' object has no attribute 'freq'

自我尝试:

在df1.index中修改freq ='B'或从df2.index中删除freq时无效

1 个答案:

答案 0 :(得分:1)

两者都需要相同的类型,因此需要DatetimeIndex.to_periodPeriodIndex.to_timestamp

d = pd.DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'])

p = pd.PeriodIndex(['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',
             '2010-01-07'], dtype='period[B]', name='Date',  freq='B')

df1 = pd.DataFrame({'a':0}, index=d)
df2 = pd.DataFrame({'b':1}, index=p)

#if need output PeriodIndex
df1.index = df1.index.to_period('B')
df = pd.concat([df1, df2], axis=1)
print (df)

              a  b
2010-01-01  NaN  1
2010-01-04  0.0  1
2010-01-05  0.0  1
2010-01-06  0.0  1
2010-01-07  0.0  1

#if need output DatetimeIndex
df2.index = df2.index.to_timestamp()
df = pd.concat([df1, df2], axis=1)
print (df)

              a  b
2010-01-01  NaN  1
2010-01-04  0.0  1
2010-01-05  0.0  1
2010-01-06  0.0  1
2010-01-07  0.0  1