{"title":"Ingest from Pandas, Polars, and Ibis","description":"Load Pandas, Polars, and Ibis tables into Altertable using DuckDB's Python client and the Altertable community extension.","order":210,"askSuggestions":["How do I append a DataFrame with columns in a different order?","How do I load a large DataFrame without running out of memory?","How do I write a Polars DataFrame to a remote table?"],"navigation":true,"body":{"code":"var Component=(()=>{var u=Object.create;var s=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var g=(a,e)=>()=>(e||a((e={exports:{}}).exports,e),e.exports),v=(a,e)=>{for(var t in e)s(a,t,{get:e[t],enumerable:!0})},i=(a,e,t,r)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let o of b(e))!E.call(a,o)&&o!==t&&s(a,o,{get:()=>e[o],enumerable:!(r=p(e,o))||r.enumerable});return a};var A=(a,e,t)=>(t=a!=null?u(_(a)):{},i(e||!a||!a.__esModule?s(t,\"default\",{value:a,enumerable:!0}):t,a)),f=a=>i(s({},\"__esModule\",{value:!0}),a);var d=g((S,c)=>{c.exports=_jsx_runtime});var D={};v(D,{default:()=>m,frontmatter:()=>w});var n=A(d()),w={title:\"Ingest from Pandas, Polars, and Ibis\",description:\"Load Pandas, Polars, and Ibis tables into Altertable using DuckDB's Python client and the Altertable community extension.\",order:210,askSuggestions:[\"How do I append a DataFrame with columns in a different order?\",\"How do I load a large DataFrame without running out of memory?\",\"How do I write a Polars DataFrame to a remote table?\"]};function l(a){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",p:\"p\",pre:\"pre\",...a.components},{DocsPreviewDocument:t,DocsPreviewSection:r}=e;return t||h(\"DocsPreviewDocument\",!0),r||h(\"DocsPreviewSection\",!0),(0,n.jsxs)(t,{anchors:\"ingest-from-pandas-polars-and-ibis connect-duckdb-to-altertable load-a-pandas-dataframe load-a-polars-dataframe load-with-ibis\",children:[(0,n.jsxs)(r,{sectionId:\"ingest-from-pandas-polars-and-ibis\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,n.jsx)(e.h1,{id:\"ingest-from-pandas-polars-and-ibis\",children:\"Ingest from 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 load local data into Altertable. DuckDB exposes registered DataFrames and Ibis tables as SQL relations, and the extension sends the resulting \",(0,n.jsx)(e.code,{children:\"CREATE TABLE AS SELECT\"}),\" or \",(0,n.jsx)(e.code,{children:\"INSERT ... SELECT\"}),\" operation to the attached Altertable catalog.\"]}),`\n`,(0,n.jsx)(e.p,{children:\"The Altertable community extension requires DuckDB 1.5.4 or later.\"}),`\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/ingest/duckdb\",children:\"Ingest with DuckDB\"}),\".\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"load-a-pandas-dataframe\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,n.jsx)(e.h2,{id:\"load-a-pandas-dataframe\",children:\"Load a Pandas DataFrame\"}),`\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.jsx)(e.p,{children:\"Register a DataFrame with a SQL name, then use it as the source of a remote table:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import pandas as pd\n\nevents = pd.DataFrame(\n    {\n        \"event_id\": [1, 2, 3],\n        \"customer_id\": [\"customer-1\", \"customer-2\", \"customer-1\"],\n        \"event_name\": [\"signed_up\", \"signed_up\", \"activated\"],\n    }\n)\n\ncon.register(\"events_df\", events)\ncon.execute(\"\"\"\n    CREATE TABLE remote.main.events AS\n    SELECT *\n    FROM events_df\n\"\"\")\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"To append another DataFrame to the existing table, use \",(0,n.jsx)(e.code,{children:\"INSERT INTO\"}),\". \",(0,n.jsx)(e.code,{children:\"BY NAME\"}),\" matches columns by name instead of relying on their order:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`new_events = pd.DataFrame(\n    {\n        \"customer_id\": [\"customer-3\"],\n        \"event_name\": [\"signed_up\"],\n        \"event_id\": [4],\n    }\n)\n\ncon.register(\"new_events_df\", new_events)\ncon.execute(\"\"\"\n    INSERT INTO remote.main.events BY NAME\n    SELECT *\n    FROM new_events_df\n\"\"\")\n`})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"load-a-polars-dataframe\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,n.jsx)(e.h2,{id:\"load-a-polars-dataframe\",children:\"Load a Polars DataFrame\"}),`\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.jsx)(e.p,{children:\"Polars DataFrames use the same DuckDB relation interface:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`import polars as pl\n\nevents = pl.DataFrame(\n    {\n        \"event_id\": [1, 2, 3],\n        \"customer_id\": [\"customer-1\", \"customer-2\", \"customer-1\"],\n        \"event_name\": [\"signed_up\", \"signed_up\", \"activated\"],\n    }\n)\n\ncon.register(\"events_df\", events)\ncon.execute(\"\"\"\n    CREATE TABLE remote.main.events AS\n    SELECT *\n    FROM events_df\n\"\"\")\n`})}),`\n`,(0,n.jsx)(e.p,{children:\"Append Polars data to an existing table in the same way:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`new_events = pl.DataFrame(\n    {\n        \"event_id\": [4],\n        \"customer_id\": [\"customer-3\"],\n        \"event_name\": [\"signed_up\"],\n    }\n)\n\ncon.register(\"new_events_df\", new_events)\ncon.execute(\"\"\"\n    INSERT INTO remote.main.events BY NAME\n    SELECT *\n    FROM new_events_df\n\"\"\")\n`})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"load-with-ibis\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,n.jsx)(e.h2,{id:\"load-with-ibis\",children:\"Load 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:[(0,n.jsx)(e.a,{href:\"https://ibis-project.org/\",children:\"Ibis\"}),\" compiles dataframe expressions to SQL and uses the same DuckDB attach workflow underneath:\"]}),`\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.jsxs)(e.p,{children:[\"Build a local table with \",(0,n.jsx)(e.code,{children:\"memtable\"}),\", then create a remote table from it:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`events = ibis.memtable(\n    {\n        \"event_id\": [1, 2, 3],\n        \"customer_id\": [\"customer-1\", \"customer-2\", \"customer-1\"],\n        \"event_name\": [\"signed_up\", \"signed_up\", \"activated\"],\n    }\n)\n\ncon.create_table(\"events\", obj=events, database=\"remote.main\", overwrite=True)\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.code,{children:'database=\"remote.main\"'}),\" targets the attached Altertable catalog and its \",(0,n.jsx)(e.code,{children:\"main\"}),\" schema.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Append rows with \",(0,n.jsx)(e.code,{children:\"insert\"}),\":\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`new_events = ibis.memtable(\n    {\n        \"customer_id\": [\"customer-3\"],\n        \"event_name\": [\"signed_up\"],\n        \"event_id\": [4],\n    }\n)\n\ncon.insert(\"events\", new_events, database=\"remote.main\")\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"When column order differs from the remote table, use \",(0,n.jsx)(e.code,{children:\"INSERT INTO ... BY NAME\"}),\" through \",(0,n.jsx)(e.code,{children:\"raw_sql\"}),\":\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-python\",children:`new_events = ibis.memtable(\n    {\n        \"customer_id\": [\"customer-3\"],\n        \"event_name\": [\"signed_up\"],\n        \"event_id\": [4],\n    }\n)\n\ncon.create_table(\"new_events_df\", obj=new_events, overwrite=True)\ncon.raw_sql(\"\"\"\n    INSERT INTO remote.main.events BY NAME\n    SELECT *\n    FROM new_events_df\n\"\"\")\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"For large tables, load data in batches so each transaction stays within your available memory and transaction limits. If you need to define the remote schema explicitly before loading, create the table with \",(0,n.jsx)(e.code,{children:\"CREATE TABLE\"}),\" and then use \",(0,n.jsx)(e.code,{children:\"INSERT INTO ... BY NAME\"}),\".\"]})]})]})}function m(a={}){let{wrapper:e}=a.components||{};return e?(0,n.jsx)(e,{...a,children:(0,n.jsx)(l,{...a})}):l(a)}function h(a,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+a+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return f(D);})();\n;return Component;"},"_id":"docs/lakehouse/ingest/pandas-polars-ibis.mdx","_raw":{"sourceFilePath":"docs/lakehouse/ingest/pandas-polars-ibis.mdx","sourceFileName":"pandas-polars-ibis.mdx","sourceFileDir":"docs/lakehouse/ingest","contentType":"mdx","flattenedPath":"docs/lakehouse/ingest/pandas-polars-ibis"},"type":"Docs","path":"/lakehouse/ingest/pandas-polars-ibis","level":3,"parent":"lakehouse/ingest"}