Skip to content

get_name_code无法正确获得数据 #51

@Vorteile

Description

@Vorteile

import qstock as qs
codes = qs.get_name_code()
运行时只有极小概率获得100条数据,其他时候会报:
KeyError: "None of [Index(['代码', '名称', '涨幅', '最新', '最高', '最低', '今开', '换手率', '量比', '市盈率', '成交量',\n '成交额', '昨收', '总市值', '流通市值', '编号', '更新时间戳'],\n dtype='object')] are in the [columns]"
具体日志:

KeyError Traceback (most recent call last)
---> 21 #codes = qs.get_name_code('ETF')

File /qstock/data/trade.py:59, in get_name(market)
46 def get_name(market='沪深A'):
47 """
48 获取某市场交易标的代码和名称
49 market表示行情名称或列表,默认沪深A股
(...)
57 '深证指数':深证系列指数市场;'ETF' ETF基金市场;'LOF' LOF 基金市场
58 """
---> 59 df=market_realtime(market)
60 names=list(df['名称'])
61 return names

File /qstock/data/trade.py:184, in market_realtime(market)
182 # 数据清洗和转换
183 df_total = df_total.rename(columns=trade_detail_dict)
--> 184 df_total = df_total[trade_detail_dict.values()]
186 df_total['ID'] = df_total['编号'].astype(str) + '.' + df_total['代码'].astype(str)
187 df_total['市场'] = df_total['编号'].astype(str).apply(lambda x: market_num_dict.get(x))

File /pandas/core/frame.py:4108, in DataFrame.getitem(self, key)
4106 if is_iterator(key):
4107 key = list(key)
-> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1]
4110 # take() does not accept boolean indexers
4111 if getattr(indexer, "dtype", None) == bool:

File /pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name)
6197 else:
6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 6200 self._raise_if_missing(keyarr, indexer, axis_name)
6202 keyarr = self.take(indexer)
6203 if isinstance(key, Index):
6204 # GH 42790 - Preserve name from an Index

File /pandas/core/indexes/base.py:6249, in Index._raise_if_missing(self, key, indexer, axis_name)
6247 if nmissing:
6248 if nmissing == len(indexer):
-> 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
6252 raise KeyError(f"{not_found} not in index")

在执行第 184 行时,df_total 中缺少了 trade_detail_dict.values() 列表中的某些或全部列名。如果 key 列表,也就是trade_detail_dict.values() 列表中哪怕有一个元素在 DataFrame 的当前列中找不到,它就会立即抛出 KeyError。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions