{"title":"Query with Pandas, Polars, and Ibis","description":"Query Altertable tables with Pandas, Polars, or Ibis through the DuckDB extension or the Python lakehouse client's Ibis backend.","order":210,"askSuggestions":["How do I join a local DataFrame with an Altertable table?","Can I use Ibis without the DuckDB extension?","How do I get query results as a Polars DataFrame?"],"navigation":true,"body":{"code":"var Component=(()=>{var p=Object.create;var s=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var g=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),_=(t,e)=>{for(var a in e)s(t,a,{get:e[a],enumerable:!0})},i=(t,e,a,r)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let o of b(e))!y.call(t,o)&&o!==a&&s(t,o,{get:()=>e[o],enumerable:!(r=m(e,o))||r.enumerable});return t};var D=(t,e,a)=>(a=t!=null?p(v(t)):{},i(e||!t||!t.__esModule?s(a,\"default\",{value:t,enumerable:!0}):a,t)),E=t=>i(s({},\"__esModule\",{value:!0}),t);var l=g((k,c)=>{c.exports=_jsx_runtime});var A={};_(A,{default:()=>u,frontmatter:()=>w});var n=D(l()),w={title:\"Query with Pandas, Polars, and Ibis\",description:\"Query Altertable tables with Pandas, Polars, or Ibis through the DuckDB extension or the Python lakehouse client's Ibis backend.\",order:210,askSuggestions:[\"How do I join a local DataFrame with an Altertable table?\",\"Can I use Ibis without the DuckDB extension?\",\"How do I get query results as a Polars DataFrame?\"]};function d(t){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",p:\"p\",pre:\"pre\",...t.components},{DocsPreviewDocument:a,DocsPreviewSection:r}=e;return a||h(\"DocsPreviewDocument\",!0),r||h(\"DocsPreviewSection\",!0),(0,n.jsxs)(a,{anchors:\"query-with-pandas-polars-and-ibis connect-duckdb-to-altertable query-into-pandas query-into-polars join-remote-tables-with-local-dataframes query-with-ibis query-with-ibis-over-http\",children:[(0,n.jsxs)(r,{sectionId:\"query-with-pandas-polars-and-ibis\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,n.jsx)(e.h1,{id:\"query-with-pandas-polars-and-ibis\",children:\"Query with Pandas, Polars, and Ibis\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"Use DuckDB's Python client and the \",(0,n.jsx)(e.a,{href:\"https://github.com/altertable-ai/duckdb-altertable\",children:\"DuckDB Altertable extension\"}),\" to query Altertable tables with Pandas, Polars, or \",(0,n.jsx)(e.a,{href:\"https://ibis-project.org/\",children:\"Ibis\"}),\". DuckDB executes SQL against the attached Altertable catalog, then returns results in the dataframe format you choose.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"The Altertable community extension requires DuckDB 1.5.4 or later. For Ibis queries over HTTP, use the \",(0,n.jsx)(e.a,{href:\"#query-with-ibis-over-http\",children:\"Python lakehouse client\"}),\" instead.\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"connect-duckdb-to-altertable\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,n.jsx)(e.h2,{id:\"connect-duckdb-to-altertable\",children:\"Connect DuckDB to Altertable\"}),`\n`,(0,n.jsx)(e.p,{children:\"Load the community extension and attach an Altertable catalog:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import os\n\nimport duckdb\n\ncon = duckdb.connect()\ncon.execute(\"INSTALL altertable FROM community\")\ncon.execute(\"LOAD altertable\")\n\ncon.execute(\n    f\"ATTACH 'user={os.environ['LAKEHOUSE_USERNAME']} \"\n    f\"password={os.environ['LAKEHOUSE_PASSWORD']} \"\n    f\"catalog={os.environ['LAKEHOUSE_CATALOG']}' \"\n    \"AS remote (TYPE ALTERTABLE)\"\n)\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"The connection uses the \",(0,n.jsx)(e.code,{children:\"LAKEHOUSE_USERNAME\"}),\", \",(0,n.jsx)(e.code,{children:\"LAKEHOUSE_PASSWORD\"}),\", and \",(0,n.jsx)(e.code,{children:\"LAKEHOUSE_CATALOG\"}),\" environment variables. For other connection options and DuckDB secrets, see \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/query/duckdb\",children:\"Query with DuckDB\"}),\".\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"query-into-pandas\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,n.jsx)(e.h2,{id:\"query-into-pandas\",children:\"Query into Pandas\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-bash\",children:`pip install 'duckdb==1.5.4' pandas\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Call \",(0,n.jsx)(e.code,{children:\".df()\"}),\" on the result of a DuckDB query to return a Pandas DataFrame:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`events = con.sql(\"\"\"\n    SELECT event_id, customer_id, event_name\n    FROM remote.main.events\n    WHERE event_name = 'activated'\n    ORDER BY event_id\n\"\"\").df()\n\nprint(events)\n`})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"query-into-polars\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,n.jsx)(e.h2,{id:\"query-into-polars\",children:\"Query into Polars\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-bash\",children:`pip install 'duckdb==1.5.4' polars pyarrow\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:\"pyarrow\"}),\" is required for DuckDB's Polars integration.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Call \",(0,n.jsx)(e.code,{children:\".pl()\"}),\" to return a Polars DataFrame:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`events = con.sql(\"\"\"\n    SELECT event_id, customer_id, event_name\n    FROM remote.main.events\n    WHERE event_name = 'activated'\n    ORDER BY event_id\n\"\"\").pl()\n\nprint(events)\n`})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"join-remote-tables-with-local-dataframes\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,n.jsx)(e.h2,{id:\"join-remote-tables-with-local-dataframes\",children:\"Join remote tables with local DataFrames\"}),`\n`,(0,n.jsx)(e.p,{children:\"Register a local DataFrame with DuckDB to use it in the same SQL query as an attached Altertable table:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import polars as pl\n\ncustomers = pl.DataFrame(\n    {\n        \"customer_id\": [\"customer-1\", \"customer-2\"],\n        \"segment\": [\"self_serve\", \"enterprise\"],\n    }\n)\n\ncon.register(\"customers_df\", customers)\nresult = con.sql(\"\"\"\n    SELECT\n        c.customer_id,\n        c.segment,\n        COUNT(*) AS event_count\n    FROM customers_df AS c\n    JOIN remote.main.events AS e\n        ON e.customer_id = c.customer_id\n    GROUP BY c.customer_id, c.segment\n    ORDER BY event_count DESC\n\"\"\").pl()\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"The same registration pattern works with Pandas DataFrames.\"}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"query-with-ibis\",anchors:\"\",ancestors:\"\",index:\"5\",children:[(0,n.jsx)(e.h2,{id:\"query-with-ibis\",children:\"Query with Ibis\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-bash\",children:`pip install 'ibis-framework[duckdb]' 'duckdb==1.5.4'\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Install \",(0,n.jsx)(e.code,{children:\"pandas\"}),\" or \",(0,n.jsx)(e.code,{children:\"polars pyarrow\"}),\" as well if you plan to call \",(0,n.jsx)(e.code,{children:\".to_pandas()\"}),\" or \",(0,n.jsx)(e.code,{children:\".to_polars()\"}),\".\"]}),`\n`,(0,n.jsx)(e.p,{children:\"Create an Ibis connection with the same attach workflow:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import os\n\nimport ibis\n\ncon = ibis.duckdb.connect()\ncon.raw_sql(\"INSTALL altertable FROM community\")\ncon.raw_sql(\"LOAD altertable\")\ncon.raw_sql(\n    f\"ATTACH 'user={os.environ['LAKEHOUSE_USERNAME']} \"\n    f\"password={os.environ['LAKEHOUSE_PASSWORD']} \"\n    f\"catalog={os.environ['LAKEHOUSE_CATALOG']}' \"\n    \"AS remote (TYPE ALTERTABLE)\"\n)\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"Reference a remote table, filter and sort with Ibis expressions, then materialize the result:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`events = con.table(\"events\", database=\"remote.main\")\nactivated = events.filter(events.event_name == \"activated\").order_by(\"event_id\")\n\nactivated.to_pandas()\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"Return a Polars DataFrame instead:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`activated.to_polars()\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Use \",(0,n.jsx)(e.code,{children:\"sql()\"}),\" when you prefer to write SQL directly:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`events = con.sql(\"\"\"\n    SELECT event_id, customer_id, event_name\n    FROM remote.main.events\n    WHERE event_name = 'activated'\n    ORDER BY event_id\n\"\"\")\n\nevents.to_pandas()\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Join a local \",(0,n.jsx)(e.code,{children:\"memtable\"}),\" to an attached Altertable table in the same expression:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`customers = ibis.memtable(\n    {\n        \"customer_id\": [\"customer-1\", \"customer-2\"],\n        \"segment\": [\"self_serve\", \"enterprise\"],\n    }\n)\n\nevents = con.table(\"events\", database=\"remote.main\")\nresult = (\n    customers.join(events, \"customer_id\")\n    .group_by(\"customer_id\", \"segment\")\n    .aggregate(event_count=events.event_id.count())\n    .order_by(ibis.desc(\"event_count\"))\n)\n\nresult.to_polars()\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"For the SQL-only workflow, including attachment options and direct table access, see \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/query/duckdb\",children:\"Query with DuckDB\"}),\".\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"query-with-ibis-over-http\",anchors:\"\",ancestors:\"\",index:\"6\",children:[(0,n.jsx)(e.h2,{id:\"query-with-ibis-over-http\",children:\"Query with Ibis over HTTP\"}),`\n`,(0,n.jsx)(e.p,{children:\"The Python lakehouse client also provides an optional Ibis backend. Install the Ibis extra:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-bash\",children:`pip install 'altertable-lakehouse[ibis]'\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"Connect directly to Altertable without loading the DuckDB extension:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import os\n\nimport ibis\n\ncon = ibis.altertable.connect(\n    username=os.environ[\"LAKEHOUSE_USERNAME\"],\n    password=os.environ[\"LAKEHOUSE_PASSWORD\"],\n    catalog=os.environ[\"LAKEHOUSE_CATALOG\"],\n    database=\"main\",\n)\n\ntry:\n    events = con.table(\"events\")\n    counts = events.group_by(\"event_name\").aggregate(event_count=events.count())\n    print(counts.execute())\nfinally:\n    con.disconnect()\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:\"catalog\"}),\" selects the Altertable catalog; \",(0,n.jsx)(e.code,{children:\"database\"}),\" selects its schema. Expressions execute remotely through the HTTP query API, and results are buffered in memory. Filter and aggregate before calling \",(0,n.jsx)(e.code,{children:\"execute()\"}),\" to limit the result size.\"]}),`\n`,(0,n.jsx)(e.p,{children:\"This backend is for reading data. Use the lakehouse client's upload and ingestion methods for writes; local memtable joins and Python UDFs are not supported. For local DataFrame joins, use the DuckDB workflow above.\"})]})]})}function u(t={}){let{wrapper:e}=t.components||{};return e?(0,n.jsx)(e,{...t,children:(0,n.jsx)(d,{...t})}):d(t)}function h(t,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+t+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return E(A);})();\n;return Component;"},"_id":"docs/lakehouse/query/pandas-polars-ibis.mdx","_raw":{"sourceFilePath":"docs/lakehouse/query/pandas-polars-ibis.mdx","sourceFileName":"pandas-polars-ibis.mdx","sourceFileDir":"docs/lakehouse/query","contentType":"mdx","flattenedPath":"docs/lakehouse/query/pandas-polars-ibis"},"type":"Docs","path":"/lakehouse/query/pandas-polars-ibis","level":3,"parent":"lakehouse/query"}