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

为何结构相似的DataFrame呈现两种不同索引类型?

看涨期权模拟中DataFrame合并问题及优化疑问

问题背景

我在基于看涨期权模拟结果绘制PnL与价格的对比图表时,需要合并提取到的两个DataFrame(terminal_price和terminal_pnl),但合并结果不符合预期。排查后确认:两者维度一致、均为pandas.core.frame.DataFrame类型,但索引类型存在差异——terminal_price的索引为object类型,terminal_pnl的索引为RangeIndex类型。

尝试按现有索引执行concat操作时得到空DataFrame,之后通过为两者添加唯一counter列,使用merge方法成功获取到目标DataFrame,代码如下:

terminal_price['counter'] = np.arange(terminal_price.shape[0])
terminal_pnl['counter'] = np.arange(terminal_pnl.shape[0])
pnl_vs_price_df = terminal_price.merge(terminal_pnl, how = 'inner', on='counter')

核心疑问

  • 是否存在更直接的合并实现方式?
  • 为何会出现索引类型的差异?
  • 如何统一两个DataFrame的索引?

测试验证

由于模拟代码过于庞大无法复现数据源,我用5步模拟进行测试:两个DataFrame外观上相似,但执行concat时会堆叠数据而非按索引并列;按索引执行merge无结果,进一步验证了索引存在差异的问题。

数据提取代码

terminal_price = dhedge_strat['path'].iloc[-1:,].T
terminal_price.rename(columns = {999 : 'term_price'}, inplace=True)
terminal_pnl = pd.DataFrame(dhedge_err_['hedge_error']).reset_index()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:17:41