Pandas拼接时报错'Length of values does not match length of index'排查
问题分析与解决:遍历字典调用dydx API拼接K线数据时的Pandas索引错误
核心问题原因
- 全局列表未重置:所有存储K线字段的列表(如
StartedAt、Low等)定义在循环外部,每次迭代币种时不会清空,导致后续迭代中列表包含之前所有币种的数据,创建dataframe时长度远超当前币种的K线条数,触发"Length of values does not match length of index"错误。 - 字段映射错误:代码中错误地将
Low列表赋值给open列,Open列表赋值给low列,导致K线数据字段对应错误。
修复方案
- 将所有K线数据列表移至循环内部,每次处理新币种前重置为空。
- 修正字段映射关系,确保open/close/high/low对应正确的列表。
- 优化DataFrame创建逻辑,直接通过字典批量生成,避免逐列赋值的冗余操作。
修改后的完整代码
import dydx3 as dy import pandas as pd import os dir = os.getcwd() client = dy.Client( host='https://api.dydx.exchange' ) markets_1m = { 'BTC-USD': f'{dir}\\markets\\BTC-USD_1MIN.csv', 'ETH-USD': f'{dir}\\markets\\ETH-USD_1MIN.csv', 'LINK-USD': f'{dir}\\markets\\LINK-USD_1MIN.csv', 'SOL-USD': f'{dir}\\markets\\SOL-USD_1MIN.csv', 'ATOM-USD': f'{dir}\\markets\\ATOM-USD_1MIN.csv', 'XMR-USD': f'{dir}\\markets\\XMR-USD_1MIN.csv', 'RUNE-USD': f'{dir}\\markets\\RUNE-USD_1MIN.csv', } def get_price_data_1m(): try: for ticker, path in markets_1m.items(): # 每次迭代重置数据列表 StartedAt = [] UpdatedAt = [] Market = [] Resolution = [] Low = [] High = [] Open = [] Close = [] BaseTokenVolume = [] Trades = [] UsdVolume = [] StartingOpenInterest = [] candles = client.public.get_candles( market=ticker, resolution='1MIN', ) for i in candles.data['candles']: StartedAt.append(i['startedAt']) UpdatedAt.append(i['updatedAt']) Market.append(i['market']) Resolution.append(i['resolution']) Low.append(i['low']) High.append(i['high']) Open.append(i['open']) Close.append(i['close']) BaseTokenVolume.append(i['baseTokenVolume']) Trades.append(i['trades']) UsdVolume.append(i['usdVolume']) StartingOpenInterest.append(i['startingOpenInterest']) # 直接用字典创建DataFrame,修正字段映射 dataframe = pd.DataFrame({ 'startedAt': StartedAt, 'updatedAt': UpdatedAt, 'market': Market, 'resolution': Resolution, 'open': Open, 'high': High, 'low': Low, 'close': Close, 'baseTokenVolume': BaseTokenVolume, 'trades': Trades, 'usdVolume': UsdVolume, 'startingOpenInterest': StartingOpenInterest }) price_history = pd.read_csv(path) # 拼接数据并去重排序 combined = pd.concat([price_history, dataframe], join='outer', ignore_index=True) combined.drop_duplicates(subset=['startedAt'], keep='first', inplace=True) combined.sort_values(by=['startedAt'], inplace=True) combined.to_csv(f'{dir}\\markets\\{ticker}_1MIN.csv', index=False) print(f"完成 {ticker} 数据更新") except Exception as e: print(f'错误: {e}') # 调用函数 get_price_data_1m()
额外优化点
- 路径拼接使用双反斜杠
\\替代单反斜杠,避免转义字符问题,也可以用os.path.join()进一步增强兼容性:path = os.path.join(dir, 'markets', f'{ticker}_1MIN.csv') - 移除了不必要的
reset_index(drop=True),因为ignore_index=True已经会重置拼接后的索引。
内容的提问来源于stack exchange,提问作者ProjectJords
相关产品推荐
相关产品推荐

