问题描述
解决方案
在 JoinQuant 聚宽平台中,获取所有概念板块列表可以使用 get_concepts() 函数。
获取所有概念板块列表
使用 get_concepts() 函数可以获取平台支持的所有概念板块信息。该函数不需要传入参数。
代码示例
from jqdata import *
# 获取所有概念板块列表
concepts_df = get_concepts()
# 打印前 10 行查看数据结构
print(concepts_df.head(10))
# 打印概念板块的总数量
print(f"概念板块总数: {len(concepts_df)}")
返回值说明
该函数返回一个 pandas.DataFrame 对象,包含以下字段:
- index: 概念代码(例如 'GN001')
- name: 概念名称(例如 '锂电池')
- start_date: 该概念板块的开始日期
典型输出示例
name start_date
GN001 锂电池 2010-12-31
GN002 稀土永磁 2010-12-31
GN003 卫星导航 2010-12-31
... ... ...
进阶使用:获取特定概念板块的成分股
获取到概念代码后,通常会配合 get_concept_stocks 函数来获取该板块下的具体股票列表。
from jqdata import *
# 1. 获取所有概念
df = get_concepts()
# 2. 假设我们要查找名称中包含"新能源"的概念
target_concepts = df[df['name'].str.contains('新能源')]
print("找到的相关概念:")
print(target_concepts)
# 3. 如果找到了相关概念,获取第一个概念代码对应的成分股
if not target_concepts.empty:
concept_code = target_concepts.index[0]
concept_name = target_concepts['name'][0]
# 获取成分股列表
stocks = get_concept_stocks(concept_code, date='2023-01-01')
print(f"\n概念【{concept_name}】({concept_code}) 的成分股示例:")
print(stocks[:5])
Q&A
Q: get_concepts() 函数需要传入日期参数吗?
A: 不需要。该函数返回的是历史上所有存在的概念板块列表,不依赖于特定日期。
Q: 如何查看某个概念板块包含哪些股票?
A: 使用 get_concept_stocks(concept_code, date=None) 函数。其中 concept_code 可以从 get_concepts() 的返回值索引中获取。
Q: 返回的 DataFrame 中索引(Index)是什么?
A: 返回的 DataFrame 的索引(Index)是概念板块的代码(如 GNxxx),可以直接用于后续查询成分股。
Q: 为什么有些概念板块获取不到成分股?
A: 请检查 get_concept_stocks 中的 date 参数。如果该概念板块在查询日期时还未成立(早于 start_date),或者已经不再维护,可能会返回空列表。建议查询日期设置为近期或概念存续期内。