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

批量从Datastream获取多国债券发行数据的实现方法问询

批量查询多国家债券数据的实现方案

核心思路

把需要查询的国家整理成一个列表,通过字符串拼接自动生成filter参数中的国家筛选条件,增删国家只需修改列表,无需手动调整查询语句。

具体实现代码

# 1. 定义需要查询的40个国家列表,直接增删即可
target_countries = [
    'Angola', 'Benin', 'Botswana',
    # 补充剩余37个国家
]

# 2. 自动生成国家筛选条件:将每个国家转为(DTSubjectName eq 'XXX')格式,用or连接
country_filter = " or ".join([f"(DTSubjectName eq '{country}')" for country in target_countries])

# 3. 拼接完整filter字符串,注意日期需加单引号修正原代码语法问题
full_filter = f"""
((DbType eq 'GOVT') and 
 ((RCSCurrencyLeaf eq 'US Dollar') or (RCSCurrencyLeaf eq 'Euro')) and
 ({country_filter} and MaturityDate gt '2000-01-01'))
"""

# 4. 执行查询并整理结果
All1 = rd.discovery.search(
    view=rd.discovery.Views.GOV_CORP_INSTRUMENTS,
    top=2000,  # 若数据量超2000,建议调大该值或分页查询
    filter=full_filter.strip(),  # 移除字符串前后空白字符
    select="DTSubjectName,RCSCountryLeaf,IssueDate,MaturityDate,RCSCurrencyLeaf,CouponRate,FaceIssuedUSD"
)

Final1 = All1.sort_values(by=['DTSubjectName', 'IssueDate'])
Final1

关键细节说明

  • 国家列表维护:所有目标国家都放在target_countries列表中,新增/删除国家仅需修改此列表,无需改动查询逻辑。
  • 条件生成:用列表推导式+join快速生成所有国家的OR条件,比手动编写效率更高且不易出错。
  • 语法修正:原代码中MaturityDate gt 2000-01-01需改为MaturityDate gt '2000-01-01',给日期添加单引号避免Datastream解析报错。
  • top参数调整:若40个国家的债券数据总数超过2000,top=2000会仅返回前2000条,建议根据实际数据量调大数值,或通过循环指定skip参数实现分页查询。

替代方案(分批次查询)

若一次性查询所有国家数据量过大导致超时或截断,可通过for循环分国家查询后合并结果:

target_countries = ['Angola', 'Benin', 'Botswana', ...]
all_data = []

for country in target_countries:
    # 单个国家的filter条件
    single_filter = f"""
    ((DbType eq 'GOVT') and 
     ((RCSCurrencyLeaf eq 'US Dollar') or (RCSCurrencyLeaf eq 'Euro')) and
     ((DTSubjectName eq '{country}') and MaturityDate gt '2000-01-01'))
    """
    # 查询单个国家数据
    country_data = rd.discovery.search(
        view=rd.discovery.Views.GOV_CORP_INSTRUMENTS,
        top=2000,
        filter=single_filter.strip(),
        select="DTSubjectName,RCSCountryLeaf,IssueDate,MaturityDate,RCSCurrencyLeaf,CouponRate,FaceIssuedUSD"
    )
    all_data.append(country_data)

# 合并所有国家的数据
Final1 = pd.concat(all_data).sort_values(by=['DTSubjectName', 'IssueDate'])
Final1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 06:03:39