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

如何将Polars DataFrame转换为Hugging Face Dataset?

Polars DataFrame与Hugging Face Dataset的转换方案

Polars DataFrame和Hugging Face Dataset均基于Arrow生态,以下是基于现有API的可行转换路径:

Polars DataFrame → Hugging Face Dataset

  • 第一步:调用Polars的to_arrow()方法,将目标DataFrame转换为pyarrow.Table对象。
  • 第二步:将pyarrow.Table序列化为pyarrow.Buffer对象,示例代码如下:
    import pyarrow as pa
    # df 为待转换的Polars DataFrame
    arrow_table = df.to_arrow()
    buffer_writer = pa.BufferOutputStream()
    arrow_table.write_to(buffer_writer)
    arrow_buffer = buffer_writer.getvalue()
    
  • 第三步:使用Datasets库的from_buffer()方法,传入上述arrow_buffer,即可生成Hugging Face Dataset对象。

Hugging Face Dataset → Polars DataFrame

反向转换同样可借助Arrow生态完成:

  • 第一步:从Dataset中提取底层的pyarrow.Table对象(Dataset基于Arrow存储,可直接通过dataset.data.table获取)。
  • 第二步:调用Polars的from_arrow()方法,传入该pyarrow.Table对象,即可得到Polars DataFrame。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:57:15