From 6a5d4dacd49f45e41c81201288b5ae23e69b8382 Mon Sep 17 00:00:00 2001 From: aszerW Date: Thu, 14 May 2026 01:15:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(datasource):=20=E4=BF=AE=E5=A4=8D=E6=BA=A2?= =?UTF-8?q?=E4=BB=B7=E7=8E=87=E8=AE=A1=E7=AE=97=E9=87=8D=E5=A4=8D=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E5=AF=BC=E8=87=B4=E7=9A=84=20reindex=20=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:长时间范围 ETF 数据获取时,出现 'cannot reindex on an axis with duplicate labels' 错误 修复: - 在 _calculate_premium_series 中先检测并去除重复日期 - price_df 和 nav_df 的索引都使用 duplicated(keep='last') 去重 - 确保 reindex 操作正常执行 --- datasource/universal_fetcher.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/datasource/universal_fetcher.py b/datasource/universal_fetcher.py index 2dae549..e64b7ab 100644 --- a/datasource/universal_fetcher.py +++ b/datasource/universal_fetcher.py @@ -247,7 +247,17 @@ class UniversalDataFetcher: """ # 对齐日期:净值用ffill填充(因为T+1公布) # 价格日期可能比净值日期多一天 - aligned_nav = nav_df['nav'].reindex(price_df.index, method='ffill') + # 先去除重复日期 + price_index = price_df.index + if price_index.has_duplicates: + price_df = price_df[~price_df.index.duplicated(keep='last')] + price_index = price_df.index + + nav_index = nav_df.index + if nav_index.has_duplicates: + nav_df = nav_df[~nav_df.index.duplicated(keep='last')] + + aligned_nav = nav_df['nav'].reindex(price_index, method='ffill') # 计算溢价率 close_prices = price_df['close']