mcpField
D
datacommons-client

datacommons-client

github.com/davila7数据与数据库33.0k225 次安装今天更新
代码仓库

关于 datacommons-client

面向 Python 开发者的 Data Commons API v2 技能,可查询统计观测值、探索知识图谱并解析实体标识符。响应可转换为 Pandas DataFrame,便于数据分析。

主要功能

  1. 01查询统计时间序列
  2. 02探索图谱属性与层级
  3. 03将名称和坐标解析为 DCID
  4. 04将观测结果转换为 Pandas DataFrame

适用场景

  1. 01比较不同地区的统计数据
  2. 02分析历史趋势
  3. 03根据名称或坐标查找实体

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/davila7/claude-code-templates --skill datacommons-client

安装后在 Claude Code 中输入/datacommons-client确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
getting_started.md
node.md
observation.md
resolve.md

详细介绍

Data Commons 客户端

概述

全面支持 Data Commons Python API v2,可用于查询统计观测数据、探索知识图谱以及解析实体标识符。Data Commons 将人口普查机构、卫生组织、环境机构及其他权威来源的数据汇总到统一的知识图谱中。

安装

安装支持 Pandas 的 Data Commons Python 客户端:

uv pip install "datacommons-client[Pandas]"

如需在不使用 Pandas 的情况下进行基本操作:

uv pip install datacommons-client

核心功能

Data Commons API 包含三个主要端点,每个端点的详细信息均见对应的参考文件:

1. Observation 端点 - 统计数据查询

查询实体的时间序列统计数据。完整文档请参阅 references/observation.md。

主要用例:

  • 获取人口、经济、卫生或环境统计数据
  • 获取历史时间序列数据,以分析趋势
  • 查询层级数据(某州的所有县、某个地区的所有国家)
  • 比较多个实体的统计数据
  • 按数据来源筛选,以确保数据一致性

常见模式:

from datacommons_client import DataCommonsClient

client = DataCommonsClient()

# Get latest population data
response = client.observation.fetch(
    variable_dcids=["Count_Person"],
    entity_dcids=["geoId/06"],  # California
    date="latest"
)

# Get time series
response = client.observation.fetch(
    variable_dcids=["UnemploymentRate_Person"],
    entity_dcids=["country/USA"],
    date="all"
)

# Query by hierarchy
response = client.observation.fetch(
    variable_dcids=["MedianIncome_Household"],
    entity_expression="geoId/06<-containedInPlace+{typeOf:County}",
    date="2020"
)
2. Node 端点 - 知识图谱探索

探索知识图谱中的实体关系和属性。完整文档请参阅 references/node.md。

主要用例:

  • 查找实体可用的属性
  • 浏览地理层级(父子关系)
  • 获取实体名称和元数据
  • 探索实体之间的关联
  • 列出图谱中的所有实体类型

常见模式:

# Discover properties
labels = client.node.fetch_property_labels(
    node_dcids=["geoId/06"],
    out=True
)

# Navigate hierarchy
children = client.node.fetch_place_children(
    node_dcids=["country/USA"]
)

# Get entity names
names = client.node.fetch_entity_names(
    node_dcids=["geoId/06", "geoId/48"]
)
3. Resolve 端点 - 实体识别

将实体名称、坐标或外部 ID 转换为 Data Commons ID(DCID)。完整文档请参阅 references/resolve.md。

主要用例:

  • 将地名转换为 DCID,以便进行查询
  • 将坐标解析为地点
  • 将 Wikidata ID 映射到 Data Commons 实体
  • 处理有歧义的实体名称

常见模式:

# Resolve by name
response = client.resolve.fetch_dcids_by_name(
    names=["California", "Texas"],
    entity_type="State"
)

# Resolve by coordinates
dcid = client.resolve.fetch_dcid_by_coordinates(
    latitude=37.7749,
    longitude=-122.4194
)

# Resolve Wikidata IDs
response = client.resolve.fetch_dcids_by_wikidata_id(
    wikidata_ids=["Q30", "Q99"]
)

典型工作流

大多数 Data Commons 查询都遵循以下模式:

  1. 解析实体(如果起始数据是名称):

    resolve_response = client.resolve.fetch_dcids_by_name(
        names=["California", "Texas"]
    )
    dcids = [r["candidates"][0]["dcid"]
             for r in resolve_response.to_dict().values()
             if r["candidates"]]
    
  2. 查找可用变量(可选):

    variables = client.observation.fetch_available_statistical_variables(
        entity_dcids=dcids
    )
    
  3. 查询统计数据:

    response = client.observation.fetch(
        variable_dcids=["Count_Person", "UnemploymentRate_Person"],
        entity_dcids=dcids,
        date="latest"
    )
    
  4. 处理结果:

    # As dictionary
    data = response.to_dict()
    
    # As Pandas DataFrame
    df = response.to_observations_as_records()
    

查找统计变量

Data Commons 中的统计变量使用特定的命名模式:

常见变量模式:

  • Count_Person - 总人口
  • Count_Person_Female - 女性人口
  • UnemploymentRate_Person - 失业率
  • Median_Income_Household - 家庭收入中位数
  • Count_Death - 死亡人数
  • Median_Age_Person - 年龄中位数

查找方法:

# Check what variables are available for an entity
available = client.observation.fetch_available_statistical_variables(
    entity_dcids=["geoId/06"]
)

# Or explore via the web interface
# https://datacommons.org/tools/statvar

使用 Pandas

所有观测响应都与 Pandas 集成:

response = client.observation.fetch(
    variable_dcids=["Count_Person"],
    entity_dcids=["geoId/06", "geoId/48"],
    date="all"
)

# Convert to DataFrame
df = response.to_observations_as_records()
# Columns: date, entity, variable, value

# Reshape for analysis
pivot = df.pivot_table(
    values='value',
    index='date',
    columns='entity'
)

API 身份验证

适用于 datacommons.org(默认):

  • 需要 API 密钥
  • 通过环境变量设置:export DC_API_KEY="your_key"
  • 或在初始化时传入:client = DataCommonsClient(api_key="your_key")
  • 在此处申请密钥:https://apikeys.datacommons.org/

适用于自定义 Data Commons 实例:

  • 无需 API 密钥
  • 指定自定义端点:client = DataCommonsClient(url="https://custom.datacommons.org")

参考文档

references/ 目录中提供了每个端点的完整文档:

  • references/observation.md:完整的 Observation API 文档,包含所有方法、参数、响应格式和常见用例
  • references/node.md:完整的 Node API 文档,介绍图谱探索、属性查询和层级导航
  • references/resolve.md:完整的 Resolve API 文档,介绍实体识别和 DCID 解析
  • references/getting_started.md:快速入门指南,包含端到端示例和常见模式

其他资源

高效使用技巧

  1. 始终从解析开始:查询数据前,先将名称转换为 DCID
  2. 使用关系表达式处理层级结构:一次查询所有子项,而不是逐个查询
  3. 先检查数据是否可用:使用 fetch_available_statistical_variables() 查看可查询的内容
  4. 利用 Pandas 集成:将响应转换为 DataFrame 以便分析
  5. 缓存解析结果:如果反复查询相同实体,请保存名称→DCID 映射
  6. 按 facet 筛选以确保一致性:使用 filter_facet_domains 确保数据来自同一来源
  7. 阅读参考文档:每个端点都在 references/ 目录中提供了详尽文档

评论

支持 Markdown