You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改COMMODITY后Pandas concat()返回空DataFrame问题咨询

问题描述

我有两个DataFrame:COMMODITY和df_new,第一个脚本中两者数据如下:

COMMODITY
date
2023-01-03    775.50
2023-01-04    745.50
2023-01-05    746.75
2023-01-06    743.50
2023-01-09    741.50
               ...  
2023-07-18    670.75
2023-07-19    727.75
2023-07-20    727.00
2023-07-21    697.50
2023-07-24    757.50
Name: value, Length: 143, dtype: float64

df_new
             Pos_Neg
date                
2019-02-08  1.451702
2019-02-20 -1.636057
2019-02-26 -0.048959
2019-03-07  0.189759
2019-03-08  0.456263
...              ...
2023-07-21  0.075729
2023-07-22  1.201473
2023-07-23  1.397099
2023-07-24  1.009405
2023-07-25 -0.298883

[858 rows x 1 columns]

使用以下代码合并可正常执行:

merger = pd.concat([COMMODITY, df_new], axis=1, join='inner')

但第二个脚本中,COMMODITY数据更新为:

COMMODITY
date
2014-01-02    209.163498
2014-01-03    208.009465
2014-01-06    209.302691
2014-01-07    206.696035
2014-01-08    205.623171
                 ...    
2023-07-20    253.973427
2023-07-21    249.276062
2023-07-24    261.691134
2023-07-25    260.329869
2023-07-26    262.250000
Name: value, Length: 2439, dtype: float64

仅修改COMMODITY后,用同样的concat代码合并返回空DataFrame。

COMMODITY的创建代码如下:

COMMODITY = pd.read_sql( price_sql, con=new_pred, parse_dates=['date_id'])
COMMODITY = COMMODITY.set_index('date')
COMMODITY = COMMODITY[::-1]
COMMODITY = COMMODITY.drop_duplicates()
COMMODITY = COMMODITY.groupby(COMMODITY.index)['value'].apply(lambda x : x.median())
    
COMMODITY.index = COMMODITY.index.strftime('%Y-%m-%d')

第一个脚本合并后的后续处理代码及结果:

merger['COMAN'] = merger['Pos_Neg']
merger['commodity'] = Target
merger['arrow'] = merger.value + (merger.Pos_Neg * 3)
merger['date'] = merger.index
merger['feature_id'] = feature_id

结果:

value   Pos_Neg     COMAN commodity       arrow        date  
date                                                                        
2023-01-03  775.50  0.577601  0.577601     Wheat  777.232802  2023-01-03   
2023-01-04  745.50  0.431001  0.431001     Wheat  746.793003  2023-01-04   
2023-01-05  746.75  0.100048  0.100048     Wheat  747.050145  2023-01-05   
2023-01-06  743.50  0.368427  0.368427     Wheat  744.605281  2023-01-06   
2023-01-09  741.50 -0.833180 -0.833180     Wheat  739.000460  2023-01-09   
...            ...       ...       ...       ...         ...         ...   
2023-07-18  670.75 -0.637585 -0.637585     Wheat  668.837244  2023-07-18   
2023-07-19  727.75  1.191043  1.191043     Wheat  731.323128  2023-07-19   
2023-07-20  727.00  0.456187  0.456187     Wheat  728.368560  2023-07-20   
2023-07-21  697.50  0.075848  0.075848     Wheat  697.727543  2023-07-21   
2023-07-24  757.50  1.009524  1.009524     Wheat  760.528572  2023-07-24   

            feature_id  
date                    
2023-01-03   554884128  
2023-01-04   554884128  
2023-01-05   554884128  
2023-01-06   554884128  
2023-01-09   554884128  
...                ...  
2023-07-18   554884128  
2023-07-19   554884128  
2023-07-20   554884128  
2023-07-21   554884128  
2023-07-24   554884128  

[142 rows x 7 columns]
问题原因

核心问题是两个DataFrame的索引数据类型不匹配:

  • COMMODITY的创建代码最后一行COMMODITY.index = COMMODITY.index.strftime('%Y-%m-%d')将原本的datetime64类型索引转换成了字符串类型
  • 而df_new的索引依然是datetime64类型

即使两者的索引值看起来完全一致(比如"2023-07-21"和datetime(2023,7,21)),但在pandas中属于不同类型的对象,无法匹配,因此join='inner'时找不到交集,返回空DataFrame。

第一次脚本能正常合并,大概率是当时COMMODITY的索引未被转换成字符串(比如代码未执行到最后一行,或者当时的索引处理逻辑不同),导致两者索引类型一致,能正常匹配。

解决方案

只需确保两个DataFrame的索引类型一致即可,有两种常用方式:

方式1:将COMMODITY的索引转回datetime类型

修改COMMODITY的创建代码,替换最后一行:

# 替换原有的 strftime 行
COMMODITY.index = pd.to_datetime(COMMODITY.index)

方式2:将df_new的索引转换成字符串类型

如果需要保持COMMODITY的索引为字符串,可以修改df_new的索引:

df_new.index = df_new.index.strftime('%Y-%m-%d')

验证方法

在合并前可以先检查两者的索引类型,确认是否匹配:

print("COMMODITY索引类型:", type(COMMODITY.index[0]))
print("df_new索引类型:", type(df_new.index[0]))

内容的提问来源于stack exchange,提问作者MateMalte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 11:18:14