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

如何从pyarrow.dataset.ParquetFileFragment对象提取分区列表?

获取ParquetFileFragment的分区信息

我有一个如下的pyarrow.dataset.ParquetFileFragment对象:

<pyarrow.dataset.ParquetFileFragment path=pq-test/Location=US-California/Industry=HT-SoftWare/dce9900c46f94ec3a8dca094cf62bd34-0.parquet partition=[Industry=HT-SoftWare, Location=US-California]>

可以通过.path获取路径,但使用.partition属性无法得到分区列表,有没有办法获取它?


在PyArrow里,ParquetFileFragment的分区信息存在.partition_expression属性里,你可以这样提取具体的分区键值:

  • 先获取分区表达式:
partition_expr = fragment.partition_expression
  • 解析表达式得到键值对:
    如果分区是字段等值匹配(最常见的场景),可以遍历表达式节点提取:
from pyarrow.dataset import is_field_ref, is_literal

partition_info = {}
# 处理多条件AND连接的情况
if hasattr(partition_expr, 'left') and hasattr(partition_expr, 'right'):
    conditions = [partition_expr.left, partition_expr.right]
else:
    conditions = [partition_expr]

for cond in conditions:
    if is_field_ref(cond.left) and is_literal(cond.right):
        partition_info[cond.left.name] = cond.right.as_py()

# 输出结果:{'Industry': 'HT-SoftWare', 'Location': 'US-California'}
print(partition_info)

如果只是需要查看分区的字符串形式,直接转成字符串即可:

print(str(fragment.partition_expression))
# 输出示例:"Industry = 'HT-SoftWare' and Location = 'US-California'"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 19:15:56