{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Import data from Parquet files\n", "\n", "Load columnar data from Parquet files into Pixeltable tables for processing and analysis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem\n", "\n", "You have data stored in Parquet format—a common format for analytics, data lakes, and ML pipelines. You need to load this data for processing with AI models or combining with other data sources.\n", "\n", "| Source | Size | Use case |\n", "|--------|------|----------|\n", "| embeddings.parquet | 1M vectors | Add to similarity search |\n", "| transactions.parquet | 10M rows | Analyze with computed columns |\n", "| features.parquet | 500K rows | Combine with media data |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\n", "\n", "**What's in this recipe:**\n", "\n", "- Import Parquet files directly into tables\n", "- Export tables to Parquet for external tools\n", "- Handle schema type overrides\n", "\n", "You use `pxt.create_table()` with a `source` parameter to create a table from a Parquet file. Pixeltable infers column types from the Parquet schema automatically." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:27.208749Z", "iopub.status.busy": "2025-12-12T02:36:27.208645Z", "iopub.status.idle": "2025-12-12T02:36:29.765271Z", "shell.execute_reply": "2025-12-12T02:36:29.764793Z" } }, "outputs": [], "source": [ "%pip install -qU pixeltable pyarrow pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create sample Parquet file\n", "\n", "First, create a sample Parquet file to demonstrate the import process:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:30.883816Z", "iopub.status.busy": "2025-12-12T02:36:30.883646Z", "iopub.status.idle": "2025-12-12T02:36:30.906815Z", "shell.execute_reply": "2025-12-12T02:36:30.906523Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
product_idnamepricecategoryin_stock
01Widget A29.99widgetsTrue
12Widget B39.99widgetsFalse
23Gadget X149.99gadgetsTrue
34Gadget Y199.99gadgetsTrue
45Tool Z79.99toolsFalse
\n", "
" ], "text/plain": [ " product_id name price category in_stock\n", "0 1 Widget A 29.99 widgets True\n", "1 2 Widget B 39.99 widgets False\n", "2 3 Gadget X 149.99 gadgets True\n", "3 4 Gadget Y 199.99 gadgets True\n", "4 5 Tool Z 79.99 tools False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import pixeltable as pxt\n", "import tempfile\n", "from pathlib import Path\n", "\n", "# Create sample data\n", "sample_data = pd.DataFrame(\n", " {\n", " 'product_id': [1, 2, 3, 4, 5],\n", " 'name': [\n", " 'Widget A',\n", " 'Widget B',\n", " 'Gadget X',\n", " 'Gadget Y',\n", " 'Tool Z',\n", " ],\n", " 'price': [29.99, 39.99, 149.99, 199.99, 79.99],\n", " 'category': ['widgets', 'widgets', 'gadgets', 'gadgets', 'tools'],\n", " 'in_stock': [True, False, True, True, False],\n", " }\n", ")\n", "\n", "# Save to temporary Parquet file\n", "temp_dir = tempfile.mkdtemp()\n", "parquet_path = Path(temp_dir) / 'products.parquet'\n", "sample_data.to_parquet(parquet_path, index=False)\n", "sample_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Parquet file\n", "\n", "Use `create_table` with the `source` parameter to create a table directly from the Parquet file:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:30.908415Z", "iopub.status.busy": "2025-12-12T02:36:30.908287Z", "iopub.status.idle": "2025-12-12T02:36:31.119099Z", "shell.execute_reply": "2025-12-12T02:36:31.118729Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata\n", "Created directory 'parquet_demo'.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a fresh directory\n", "pxt.drop_dir('parquet_demo', force=True)\n", "pxt.create_dir('parquet_demo')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:31.120907Z", "iopub.status.busy": "2025-12-12T02:36:31.120797Z", "iopub.status.idle": "2025-12-12T02:36:31.927305Z", "shell.execute_reply": "2025-12-12T02:36:31.926870Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created table 'products'.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "Inserting rows into `products`: 0 rows [00:00, ? rows/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "Inserting rows into `products`: 5 rows [00:00, 653.18 rows/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Inserted 5 rows with 0 errors.\n" ] } ], "source": [ "# Import Parquet file into a new table\n", "products = pxt.create_table(\n", " 'parquet_demo/products', source=str(parquet_path)\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:31.929771Z", "iopub.status.busy": "2025-12-12T02:36:31.929265Z", "iopub.status.idle": "2025-12-12T02:36:31.942983Z", "shell.execute_reply": "2025-12-12T02:36:31.942564Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
product_idnamepricecategoryin_stock
1Widget A29.99widgetsTrue
2Widget B39.99widgetsFalse
3Gadget X149.99gadgetsTrue
4Gadget Y199.99gadgetsTrue
5Tool Z79.99toolsFalse
" ], "text/plain": [ " product_id name price category in_stock\n", "0 1 Widget A 29.99 widgets True\n", "1 2 Widget B 39.99 widgets False\n", "2 3 Gadget X 149.99 gadgets True\n", "3 4 Gadget Y 199.99 gadgets True\n", "4 5 Tool Z 79.99 tools False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View imported data\n", "products.collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add computed columns\n", "\n", "Once imported, you can add computed columns like any other Pixeltable table:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:31.945115Z", "iopub.status.busy": "2025-12-12T02:36:31.944975Z", "iopub.status.idle": "2025-12-12T02:36:31.985018Z", "shell.execute_reply": "2025-12-12T02:36:31.984613Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 5 column values with 0 errors.\n" ] }, { "data": { "text/plain": [ "5 rows updated, 10 values computed." ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add a computed column for discounted price\n", "products.add_computed_column(sale_price=products.price * 0.9)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:31.987011Z", "iopub.status.busy": "2025-12-12T02:36:31.986843Z", "iopub.status.idle": "2025-12-12T02:36:31.997561Z", "shell.execute_reply": "2025-12-12T02:36:31.997127Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namepricesale_price
Widget A29.9926.991
Widget B39.9935.991
Gadget X149.99134.991
Gadget Y199.99179.991
Tool Z79.9971.991
" ], "text/plain": [ " name price sale_price\n", "0 Widget A 29.99 26.991\n", "1 Widget B 39.99 35.991\n", "2 Gadget X 149.99 134.991\n", "3 Gadget Y 199.99 179.991\n", "4 Tool Z 79.99 71.991" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View with computed column\n", "products.select(\n", " products.name, products.price, products.sale_price\n", ").collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import with primary key\n", "\n", "Specify a primary key when you need upsert behavior or unique constraints:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:31.999356Z", "iopub.status.busy": "2025-12-12T02:36:31.999243Z", "iopub.status.idle": "2025-12-12T02:36:32.080287Z", "shell.execute_reply": "2025-12-12T02:36:32.079937Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created table 'products_with_pk'.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "Inserting rows into `products_with_pk`: 0 rows [00:00, ? rows/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "Inserting rows into `products_with_pk`: 5 rows [00:00, 1548.97 rows/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Inserted 5 rows with 0 errors.\n" ] } ], "source": [ "# Import with a primary key\n", "products_pk = pxt.create_table(\n", " 'parquet_demo/products_with_pk',\n", " source=str(parquet_path),\n", " primary_key='product_id',\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:32.082239Z", "iopub.status.busy": "2025-12-12T02:36:32.082119Z", "iopub.status.idle": "2025-12-12T02:36:32.094379Z", "shell.execute_reply": "2025-12-12T02:36:32.093921Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
product_idnamepricecategoryin_stock
1Widget A29.99widgetsTrue
2Widget B39.99widgetsFalse
3Gadget X149.99gadgetsTrue
4Gadget Y199.99gadgetsTrue
5Tool Z79.99toolsFalse
" ], "text/plain": [ " product_id name price category in_stock\n", "0 1 Widget A 29.99 widgets True\n", "1 2 Widget B 39.99 widgets False\n", "2 3 Gadget X 149.99 gadgets True\n", "3 4 Gadget Y 199.99 gadgets True\n", "4 5 Tool Z 79.99 tools False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View the table\n", "products_pk.collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export table to Parquet\n", "\n", "Export your processed data back to Parquet for use with other toolee" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:32.096231Z", "iopub.status.busy": "2025-12-12T02:36:32.096113Z", "iopub.status.idle": "2025-12-12T02:36:32.108008Z", "shell.execute_reply": "2025-12-12T02:36:32.107618Z" } }, "outputs": [], "source": [ "# Export to Parquet (note: image columns require inline_images=True)\n", "export_path = Path(temp_dir) / 'exported_products'\n", "\n", "pxt.io.export_parquet(\n", " products.select(products.name, products.price, products.sale_price),\n", " parquet_path=export_path,\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-12-12T02:36:32.110166Z", "iopub.status.busy": "2025-12-12T02:36:32.110036Z", "iopub.status.idle": "2025-12-12T02:36:32.116236Z", "shell.execute_reply": "2025-12-12T02:36:32.115860Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namepricesale_price
0Widget A29.99000026.990999
1Widget B39.99000235.991001
2Gadget X149.990005134.990997
3Gadget Y199.990005179.990997
4Tool Z79.98999871.990997
\n", "
" ], "text/plain": [ " name price sale_price\n", "0 Widget A 29.990000 26.990999\n", "1 Widget B 39.990002 35.991001\n", "2 Gadget X 149.990005 134.990997\n", "3 Gadget Y 199.990005 179.990997\n", "4 Tool Z 79.989998 71.990997" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Verify export by reading back\n", "import pyarrow.parquet as pq\n", "\n", "exported_table = pq.read_table(export_path)\n", "exported_table.to_pandas()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explanation\n", "\n", "**When to use Parquet import:**\n", "\n", "| Scenario | Recommendation |\n", "|----------|----------------|\n", "| Data lake / analytics data | Use `create_table(source=path)` |\n", "| ML feature stores | Use `create_table` with `primary_key` |\n", "| Small datasets | Consider CSV for simplicity |\n", "| Streaming data | Use direct `insert()` instead |\n", "\n", "**Key features:**\n", "\n", "- Automatic schema inference from Parquet metadata\n", "- Support for partitioned datasets (directory of files)\n", "- Export with `pxt.io.export_parquet` for interoperability\n", "- Primary key support for upsert workflows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## See also\n", "\n", "- [Import CSV files](https://docs.pixeltable.com/howto/cookbooks/data/data-import-csv) - For CSV and Excel imports\n", "- [Import JSON files](https://docs.pixeltable.com/howto/cookbooks/data/data-import-json) - For JSON data" ] } ], "metadata": { "kernelspec": { "display_name": "pixeltable", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" } }, "nbformat": 4, "nbformat_minor": 2 }