跳转到主要内容

便携式Python数据框库

项目描述

Ibis

Documentation status Project chat Anaconda badge PyPI Build status Build status Codecov branch

什么是 Ibis?

Ibis 是一个便携式 Python 数据帧库

  • 快速本地数据帧(默认通过 DuckDB)
  • 懒式数据帧表达式
  • 交互式模式用于迭代数据探索
  • 组合 Python 数据帧和 SQL 代码
  • 使用相同的 API 接口进行 20+ 后端
  • 通过更改一行代码在本地迭代并远程部署

查看 "为什么选择 Ibis?" 文档了解更多信息。

入门

您可以使用 pip install 安装 Ibis 以及后端和示例数据

pip install 'ibis-framework[duckdb,examples]'

💡 提示

查看 安装指南 了解更多安装选项。

然后使用 Ibis

>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.examples.penguins.fetch()
>>> t
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
 species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
 string   string     float64         float64        int64              int64        string  int64 
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
 Adelie   Torgersen            39.1           18.7                181         3750  male     2007 
 Adelie   Torgersen            39.5           17.4                186         3800  female   2007 
 Adelie   Torgersen            40.3           18.0                195         3250  female   2007 
 Adelie   Torgersen            NULL           NULL               NULL         NULL  NULL     2007 
 Adelie   Torgersen            36.7           19.3                193         3450  female   2007 
 Adelie   Torgersen            39.3           20.6                190         3650  male     2007 
 Adelie   Torgersen            38.9           17.8                181         3625  female   2007 
 Adelie   Torgersen            39.2           19.6                195         4675  male     2007 
 Adelie   Torgersen            34.1           18.1                193         3475  NULL     2007 
 Adelie   Torgersen            42.0           20.2                190         4250  NULL     2007 
                                                                                          
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘
>>> g = t.group_by("species", "island").agg(count=t.count()).order_by("count")
>>> g
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
 species    island     count 
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
 string     string     int64 
├───────────┼───────────┼───────┤
 Adelie     Biscoe        44 
 Adelie     Torgersen     52 
 Adelie     Dream         56 
 Chinstrap  Dream         68 
 Gentoo     Biscoe       124 
└───────────┴───────────┴───────┘

💡 提示

查看 入门教程 以全面了解 Ibis。

Python + SQL:更佳组合

对于大多数后端,Ibis 通过将数据帧表达式编译成 SQL 来工作

>>> ibis.to_sql(g)
SELECT
  "t1"."species",
  "t1"."island",
  "t1"."count"
FROM (
  SELECT
    "t0"."species",
    "t0"."island",
    COUNT(*) AS "count"
  FROM "penguins" AS "t0"
  GROUP BY
    1,
    2
) AS "t1"
ORDER BY
  "t1"."count" ASC

您可以混合 SQL 和 Python 代码

>>> a = t.sql("SELECT species, island, count(*) AS count FROM penguins GROUP BY 1, 2")
>>> a
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
 species    island     count 
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
 string     string     int64 
├───────────┼───────────┼───────┤
 Adelie     Torgersen     52 
 Adelie     Biscoe        44 
 Adelie     Dream         56 
 Gentoo     Biscoe       124 
 Chinstrap  Dream         68 
└───────────┴───────────┴───────┘
>>> b = a.order_by("count")
>>> b
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
 species    island     count 
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
 string     string     int64 
├───────────┼───────────┼───────┤
 Adelie     Biscoe        44 
 Adelie     Torgersen     52 
 Adelie     Dream         56 
 Chinstrap  Dream         68 
 Gentoo     Biscoe       124 
└───────────┴───────────┴───────┘

这使您能够结合 Python 的灵活性和现代 SQL 的规模和性能。

后端

Ibis 支持 20+ 后端

工作原理

大多数 Python 数据帧与其执行引擎紧密耦合。许多数据库仅支持 SQL,没有 Python API。Ibis 通过提供一个用于 Python 数据操作的通用 API 并将其编译为后端的本地语言来解决此问题。这意味着您只需学习一个 API,就可以在所有受支持的后端(执行引擎)中使用它。

Ibis 广泛支持两种类型后端

  1. 生成 SQL 的后端
  2. 生成 DataFrame 的后端

Ibis backend types

便携性

要使用不同的后端,您可以设置 Ibis 使用的后端

>>> ibis.set_backend("duckdb")
>>> ibis.set_backend("polars")
>>> ibis.set_backend("datafusion")

通常,您会创建一个连接对象

>>> con = ibis.duckdb.connect()
>>> con = ibis.polars.connect()
>>> con = ibis.datafusion.connect()

并在该后端中处理表

>>> con.list_tables()
['penguins']
>>> t = con.table("penguins")

您还可以读取 CSV 或 Apache Parquet 等常见文件格式

>>> t = con.read_csv("penguins.csv")
>>> t = con.read_parquet("penguins.parquet")

这允许您通过更改一行代码在本地迭代并远程部署。

💡 提示

查看 关于在 DuckDB 和 BigQuery 上使用相同代码的博客

社区和贡献

Ibis 是一个开源项目,欢迎社区中的任何人贡献。

通过在 GitHub 上互动或在 Zulip 上与我们聊天加入我们的社区。

获取更多信息,请访问 https://ibis-project.org/

项目详情


发布历史 发布通知 | RSS 源

下载文件

下载适用于您的平台的文件。如果您不确定选择哪一个,请了解更多关于 安装软件包 的信息。

源代码发行版

ibis_framework-9.5.0.tar.gz (1.3 MB 查看哈希值)

上传时间 源代码

构建发行版

ibis_framework-9.5.0-py3-none-any.whl (2.0 MB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面