Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用不同类型的索引(一个是DatetimeIndex,另一个是PeriodIndex)组合两个数据帧?_Python_Pandas - Fatal编程技术网

Python 如何使用不同类型的索引(一个是DatetimeIndex,另一个是PeriodIndex)组合两个数据帧?

Python 如何使用不同类型的索引(一个是DatetimeIndex,另一个是PeriodIndex)组合两个数据帧?,python,pandas,Python,Pandas,我在pd.concat两个具有不同索引类型(一个是DatetimeIndex,另一个是PeriodIndex)的dfs时遇到了一个错误 df1.索引: DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'...], dtype='datetime64[ns]', length=2022, freq=None) PeriodIndex(['2010-01-01', '2010-01-

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

df1.索引:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'...],
              dtype='datetime64[ns]', length=2022, freq=None)
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'
df2.索引:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'...],
              dtype='datetime64[ns]', length=2022, freq=None)
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'
错误消息:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'...],
              dtype='datetime64[ns]', length=2022, freq=None)
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时不起作用。index和df2.index中的类型都需要相同,因此需要或:



你想按行还是按列合并?按行合并,所以pd.concat([df1,df2],axis=1)@Richard-不客气!如果我的回答有帮助,别忘了。谢谢