1. Structure
  2. Tables
  3. Quickstart
  • Home
  • What is TileDB?
  • Get Started
  • Explore Content
  • Accounts
    • Individual Accounts
      • Apply for the Free Tier
      • Profile
        • Overview
        • Cloud Credentials
        • Storage Paths
        • REST API Tokens
        • Credits
    • Organization Admins
      • Create an Organization
      • Profile
        • Overview
        • Members
        • Cloud Credentials
        • Storage Paths
        • Billing
      • API Tokens
    • Organization Members
      • Organization Invitations
      • Profile
        • Overview
        • Members
        • Cloud Credentials
        • Storage Paths
        • Billing
      • API Tokens
  • Catalog
    • Introduction
    • Data
      • Arrays
      • Tables
      • Single-Cell (SOMA)
      • Genomics (VCF)
      • Biomedical Imaging
      • Vector Search
      • Files
    • Code
      • Notebooks
      • Dashboards
      • User-Defined Functions
      • Task Graphs
      • ML Models
    • Groups
    • Marketplace
    • Search
  • Collaborate
    • Introduction
    • Organizations
    • Access Control
      • Introduction
      • Share Assets
      • Asset Permissions
      • Public Assets
    • Logging
    • Marketplace
  • Analyze
    • Introduction
    • Slice Data
    • Multi-Region Redirection
    • Notebooks
      • Launch a Notebook
      • Usage
      • Widgets
      • Notebook Image Dependencies
    • Dashboards
      • Dashboards
      • Streamlit
    • Preview
    • User-Defined Functions
    • Task Graphs
    • Serverless SQL
    • Monitor
      • Task Log
      • Task Graph Log
  • Scale
    • Introduction
    • Task Graphs
    • API Usage
  • Structure
    • Why Structure Is Important
    • Arrays
      • Introduction
      • Quickstart
      • Foundation
        • Array Data Model
        • Key Concepts
          • Storage
            • Arrays
            • Dimensions
            • Attributes
            • Cells
            • Domain
            • Tiles
            • Data Layout
            • Compression
            • Encryption
            • Tile Filters
            • Array Schema
            • Schema Evolution
            • Fragments
            • Fragment Metadata
            • Commits
            • Indexing
            • Array Metadata
            • Datetimes
            • Groups
            • Object Stores
          • Compute
            • Writes
            • Deletions
            • Consolidation
            • Vacuuming
            • Time Traveling
            • Reads
            • Query Conditions
            • Aggregates
            • User-Defined Functions
            • Distributed Compute
            • Concurrency
            • Parallelism
        • Storage Format Spec
      • Tutorials
        • Basics
          • Basic Dense Array
          • Basic Sparse Array
          • Array Metadata
          • Compression
          • Encryption
          • Data Layout
          • Tile Filters
          • Datetimes
          • Multiple Attributes
          • Variable-Length Attributes
          • String Dimensions
          • Nullable Attributes
          • Multi-Range Reads
          • Query Conditions
          • Aggregates
          • Deletions
          • Catching Errors
          • Configuration
          • Basic S3 Example
          • Basic TileDB Cloud
          • fromDataFrame
          • Palmer Penguins
        • Advanced
          • Schema Evolution
          • Advanced Writes
            • Write at a Timestamp
            • Get Fragment Info
            • Consolidation
              • Fragments
              • Fragment List
              • Consolidation Plan
              • Commits
              • Fragment Metadata
              • Array Metadata
            • Vacuuming
              • Fragments
              • Commits
              • Fragment Metadata
              • Array Metadata
          • Advanced Reads
            • Get Fragment Info
            • Time Traveling
              • Introduction
              • Fragments
              • Array Metadata
              • Schema Evolution
          • Array Upgrade
          • Backends
            • Amazon S3
            • Azure Blob Storage
            • Google Cloud Storage
            • MinIO
            • Lustre
          • Virtual Filesystem
          • User-Defined Functions
          • Distributed Compute
          • Result Estimation
          • Incomplete Queries
        • Management
          • Array Schema
          • Groups
          • Object Management
        • Performance
          • Summary of Factors
          • Dense vs. Sparse
          • Dimensions vs. Attributes
          • Compression
          • Tiling and Data Layout
          • Tuning Writes
          • Tuning Reads
      • API Reference
    • Tables
      • Introduction
      • Quickstart
      • Foundation
        • Data Model
        • Key Concepts
          • Indexes
          • Columnar Storage
          • Compression
          • Data Manipulation
          • Optimize Tables
          • ACID
          • Serverless SQL
          • SQL Connectors
          • Dataframes
          • CSV Ingestion
      • Tutorials
        • Basics
          • Ingestion with SQL
          • CSV Ingestion
          • Basic S3 Example
          • Running Locally
        • Advanced
          • Scalable Ingestion
          • Scalable Queries
      • API Reference
    • AI & ML
      • Vector Search
        • Introduction
        • Quickstart
        • Foundation
          • Data Model
          • Key Concepts
            • Vector Search
            • Vector Databases
            • Algorithms
            • Distance Metrics
            • Updates
            • Deployment Methods
            • Architecture
            • Distributed Compute
          • Storage Format Spec
        • Tutorials
          • Basics
            • Ingestion & Querying
            • Updates
            • Deletions
            • Basic S3 Example
            • Running Locally
          • Advanced
            • Versioning
            • Time Traveling
            • Consolidation
            • Distributed Compute
            • RAG LLM
            • LLM Memory
            • File Search
            • Image Search
            • Protein Search
          • Performance
        • API Reference
      • ML Models
        • Introduction
        • Quickstart
        • Foundation
          • Basics
          • Storage
          • Cloud Execution
          • Why TileDB for Machine Learning
        • Tutorials
          • Ingestion
            • Data Ingestion
              • Dense Datasets
              • Sparse Datasets
            • ML Model Ingestion
          • Management
            • Array Schema
            • Machine Learning: Groups
            • Time Traveling
    • Life Sciences
      • Single-cell
        • Introduction
        • Quickstart
        • Foundation
          • Data Model
          • Key Concepts
            • Data Structures
            • Use of Apache Arrow
            • Join IDs
            • State Management
            • TileDB Cloud URIs
          • SOMA API Specification
        • Tutorials
          • Data Ingestion
          • Bulk Ingestion Tutorial
          • Data Access
          • Distributed Compute
          • Basic S3 Example
          • Multi-Experiment Queries
          • Appending Data to a SOMA Experiment
          • Add New Measurements
          • SQL Queries
          • Running Locally
          • Shapes in TileDB-SOMA
          • Drug Discovery App
        • Spatial
          • Introduction
          • Foundation
            • Spatial Data Model
            • Data Structures
          • Tutorials
            • Spatial Data Ingestion
            • Access Spatial Data
            • Manage Coordinate Spaces
        • API Reference
      • Population Genomics
        • Introduction
        • Quickstart
        • Foundation
          • Data Model
          • Key Concepts
            • The N+1 Problem
            • Architecture
            • Arrays
            • Ingestion
            • Reads
            • Variant Statistics
            • Annotations
            • User-Defined Functions
            • Tables and SQL
            • Distributed Compute
          • Storage Format Spec
        • Tutorials
          • Basics
            • Basic Ingestion
            • Basic Queries
            • Export to VCF
            • Add New Samples
            • Deleting Samples
            • Basic S3 Example
            • Basic TileDB Cloud
          • Advanced
            • Scalable Ingestion
            • Scalable Queries
            • Query Transforms
            • Handling Large Queries
            • Annotations
              • Finding Annotations
              • Embedded Annotations
              • External Annotations
              • Annotation VCFs
              • Ingesting Annotations
            • Variant Statistics
            • Tables and SQL
            • User-Defined Functions
            • Sample Metadata
            • Split VCF
          • Performance
        • API Reference
          • Command Line Interface
          • Python API
          • Cloud API
      • Biomedical Imaging
        • Introduction
        • Foundation
          • Data Model
          • Key Concepts
            • Arrays
            • Ingestion
            • Reads
            • User Defined Functions
          • Storage Format Spec
        • Quickstart
        • Tutorials
          • Basics
            • Ingestion
            • Read
              • OpenSlide
              • TileDB-Py
          • Advanced
            • Batched Ingestion
            • Chunked Ingestion
            • Machine Learning
              • PyTorch
            • Napari
    • Files
  • API Reference
  • Self-Hosting
    • Installation
    • Upgrades
    • Administrative Tasks
    • Image Customization
      • Customize User-Defined Function Images
      • AWS ECR Container Registry
      • Customize Jupyter Notebook Images
    • Single Sign-On
      • Configure Single Sign-On
      • OpenID Connect
      • Okta SCIM
      • Microsoft Entra
  • Glossary

On this page

  • Setup
  • Ingestion
  • Read data using dataframes
  • Read data using SQL
  • Clean up
  1. Structure
  2. Tables
  3. Quickstart

Tables Quickstart

tables
quickstart
tutorials
python
This tutorial covers the basics of working with tabular data using TileDB.
How to run this tutorial

We recommend running this tutorial, as well as the other tutorials in the Tutorials section, inside TileDB Cloud. By using TileDB Cloud, you can experiment while avoiding all the installation, deployment, and configuration hassles. Sign up for the free tier, spin up a TileDB Cloud notebook with a Python kernel, and follow the tutorial instructions. If you wish to learn how to run tutorials locally on your machine, read the Tutorials: Running Locally tutorial.

This tutorial offers a rapid introduction to TileDB’s tabular support and its capabilities. It covers the following topics:

  • Ingest data from a CSV file into a new TileDB array.
  • Run dataframe queries with a pandas-like API, retrieving the results in a pandas dataframe.
  • Run SQL queries and return the results in a pandas dataframe.

Setup

Start by importing the libraries used in this tutorial, setting the URIs you will use throughout this tutorial, and cleaning up any older data with the same name.

import warnings

import tiledb

warnings.filterwarnings("ignore")
import os.path
import shutil

import pandas as pd
import tiledb.sql

# Print library versions
print("TileDB core version: {}".format(tiledb.libtiledb.version()))
print("TileDB-Py version: {}".format(tiledb.version()))
print("TileDB-SQL version: {}".format(tiledb.sql.version))

# Set CSV dataset URI
table_uri = os.path.expanduser("~/my_table")
csv_uri = (
    "s3://tiledb-inc-demo-data/examples/notebooks/nyc_yellow_tripdata/taxi_first_10.csv"
)

# Define config values and context
cfg = tiledb.Config({"vfs.s3.no_sign_request": "true", "vfs.s3.region": "us-east-1"})
ctx = tiledb.Ctx(cfg)

# Clean up past data
if os.path.exists(table_uri):
    shutil.rmtree(table_uri)

Ingestion

This process will ingest some small CSV data directly from a public S3 bucket into a local TileDB array, without needing to download the source CSV files beforehand. The ingestion should take about a few seconds from your laptop. You will use a subset from the latest New York City Taxi and Limousine Commission Trip Record Data dataset.

You can ingest with a single command, without needing to define the tabular schema beforehand:

tiledb.from_csv(
    table_uri,  # TileDB array to create
    csv_uri,  # CSV to load
    ctx=ctx,  # Context
    parse_dates=["tpep_dropoff_datetime", "tpep_pickup_datetime"],
)  # Parse these fields are datetimes

You need to pass the following cfg options into ctx to ingest directly from a public S3 bucket:

  1. "vfs.s3.no_sign_request": Set to True to reduce the cost of accessing public data by removing the need for AWS access credentials.
  2. "vfs.s3.region": Set to us-east-1 to match the location of the public TileDB demo data. This avoids file access issues caused by a different default region in a local environment.

First, prepare the table for reading, so that you can then inspect its schema and run queries.

# Open the table in read mode
table = tiledb.open(table_uri, mode="r")

Inspect the schema of the underlying array on which this particular table is based:

# Print the underlying array schema
print(table.schema)
ArraySchema(
  domain=Domain(*[
    Dim(name='__tiledb_rows', domain=(0, 9), tile=10, dtype='uint64', filters=FilterList([ZstdFilter(level=-1), ])),
  ]),
  attrs=[
    Attr(name='VendorID', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='tpep_pickup_datetime', dtype='datetime64[ns]', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='tpep_dropoff_datetime', dtype='datetime64[ns]', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='passenger_count', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='trip_distance', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='RatecodeID', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='store_and_fwd_flag', dtype='<U0', var=True, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='PULocationID', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='DOLocationID', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='payment_type', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='fare_amount', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='extra', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='mta_tax', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='tip_amount', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='tolls_amount', dtype='int64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='improvement_surcharge', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='total_amount', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
    Attr(name='congestion_surcharge', dtype='float64', var=False, nullable=False, enum_label=None, filters=FilterList([ZstdFilter(level=-1), ])),
  ],
  cell_order='row-major',
  tile_order='row-major',
  sparse=False,
)

TileDB materializes the created tabular dataset as a directory on your local storage and models the table as a dense TileDB array with a row_id dimension added. Ingesting a CSV by default produces a dense array with the dimension being the row ID. You can run !tree {table_uri} to see the file hierarchy inside the dataset directory. For more details on the meaning of those different TileDB objects and the different ways TileDB arrays can model tabular data, visit the Data Model section.

Read data using dataframes

You can read data into a pandas dataframe with the .df[] method, which allows setting the dimensions on which to slice:

# Read entire dataset into a pandas dataframe
df = table.df[:]  # Equivalent to: A.df[0:9]
df
VendorID tpep_pickup_datetime tpep_dropoff_datetime passenger_count trip_distance RatecodeID store_and_fwd_flag PULocationID DOLocationID payment_type fare_amount extra mta_tax tip_amount tolls_amount improvement_surcharge total_amount congestion_surcharge
0 1 2020-01-01 00:28:15 2020-01-01 00:33:03 1 1.20 1 N 238 239 1 6.00 3.0 0.5 1.47 0 0.3 11.27 2.5
1 1 2020-01-01 00:35:39 2020-01-01 00:43:04 1 1.20 1 N 239 238 1 7.00 3.0 0.5 1.50 0 0.3 12.30 2.5
2 1 2020-01-01 00:47:41 2020-01-01 00:53:52 1 0.60 1 N 238 238 1 6.00 3.0 0.5 1.00 0 0.3 10.80 2.5
3 1 2020-01-01 00:55:23 2020-01-01 01:00:14 1 0.80 1 N 238 151 1 5.50 0.5 0.5 1.36 0 0.3 8.16 0.0
4 2 2020-01-01 00:01:58 2020-01-01 00:04:16 1 0.00 1 N 193 193 2 3.50 0.5 0.5 0.00 0 0.3 4.80 0.0
5 2 2020-01-01 00:09:44 2020-01-01 00:10:37 1 0.03 1 N 7 193 2 2.50 0.5 0.5 0.00 0 0.3 3.80 0.0
6 2 2020-01-01 00:39:25 2020-01-01 00:39:29 1 0.00 1 N 193 193 1 2.50 0.5 0.5 0.01 0 0.3 3.81 0.0
7 2 2019-12-18 15:27:49 2019-12-18 15:28:59 1 0.00 5 N 193 193 1 0.01 0.0 0.0 0.00 0 0.3 2.81 2.5
8 2 2019-12-18 15:30:35 2019-12-18 15:31:35 4 0.00 1 N 193 193 1 2.50 0.5 0.5 0.00 0 0.3 6.30 2.5
9 1 2020-01-01 00:29:01 2020-01-01 00:40:28 2 0.70 1 N 246 48 1 8.00 3.0 0.5 2.35 0 0.3 14.15 2.5

To slice a subset of rows, pass the desired range in the .df[] method:

# Pass a specific range in for the row_id to subset select
table.df[3:5]
VendorID tpep_pickup_datetime tpep_dropoff_datetime passenger_count trip_distance RatecodeID store_and_fwd_flag PULocationID DOLocationID payment_type fare_amount extra mta_tax tip_amount tolls_amount improvement_surcharge total_amount congestion_surcharge
3 1 2020-01-01 00:55:23 2020-01-01 01:00:14 1 0.80 1 N 238 151 1 5.5 0.5 0.5 1.36 0 0.3 8.16 0.0
4 2 2020-01-01 00:01:58 2020-01-01 00:04:16 1 0.00 1 N 193 193 2 3.5 0.5 0.5 0.00 0 0.3 4.80 0.0
5 2 2020-01-01 00:09:44 2020-01-01 00:10:37 1 0.03 1 N 7 193 2 2.5 0.5 0.5 0.00 0 0.3 3.80 0.0

Read data using SQL

Along with dataframe APIs, TileDB also support ANSI SQL via an integration with MariaDB. TileDB SQL is DBI-compliant and offers the ability to create a connection and pass it to any system, such as pandas.

# Query with SQL
db = tiledb.sql.connect(init_command="SET GLOBAL time_zone='+00:00'")
pd.read_sql(sql=f"SELECT * FROM `{table_uri}`", con=db)
__tiledb_rows congestion_surcharge improvement_surcharge tolls_amount tip_amount VendorID payment_type DOLocationID mta_tax tpep_pickup_datetime tpep_dropoff_datetime passenger_count trip_distance RatecodeID store_and_fwd_flag PULocationID total_amount fare_amount extra
0 0 2.5 0.3 0 1.47 1 1 239 0.5 2020-01-01 00:28:15 2020-01-01 00:33:03 1 1.20 1 N 238 11.27 6.00 3.0
1 1 2.5 0.3 0 1.50 1 1 238 0.5 2020-01-01 00:35:39 2020-01-01 00:43:04 1 1.20 1 N 239 12.30 7.00 3.0
2 2 2.5 0.3 0 1.00 1 1 238 0.5 2020-01-01 00:47:41 2020-01-01 00:53:52 1 0.60 1 N 238 10.80 6.00 3.0
3 3 0.0 0.3 0 1.36 1 1 151 0.5 2020-01-01 00:55:23 2020-01-01 01:00:14 1 0.80 1 N 238 8.16 5.50 0.5
4 4 0.0 0.3 0 0.00 2 2 193 0.5 2020-01-01 00:01:58 2020-01-01 00:04:16 1 0.00 1 N 193 4.80 3.50 0.5
5 5 0.0 0.3 0 0.00 2 2 193 0.5 2020-01-01 00:09:44 2020-01-01 00:10:37 1 0.03 1 N 7 3.80 2.50 0.5
6 6 0.0 0.3 0 0.01 2 1 193 0.5 2020-01-01 00:39:25 2020-01-01 00:39:29 1 0.00 1 N 193 3.81 2.50 0.5
7 7 2.5 0.3 0 0.00 2 1 193 0.0 2019-12-18 15:27:49 2019-12-18 15:28:59 1 0.00 5 N 193 2.81 0.01 0.0
8 8 2.5 0.3 0 0.00 2 1 193 0.5 2019-12-18 15:30:35 2019-12-18 15:31:35 4 0.00 1 N 193 6.30 2.50 0.5
9 9 2.5 0.3 0 2.35 1 1 48 0.5 2020-01-01 00:29:01 2020-01-01 00:40:28 2 0.70 1 N 246 14.15 8.00 3.0

TileDB offers full support for joins, group bys, aggregates, and more.

# A more complex SQL query
db = tiledb.sql.connect(init_command="SET GLOBAL time_zone='+00:00'")
pd.read_sql(
    sql=f"""SELECT COUNT(*) as trip_count, 
                        MAX(total_amount) as max_total_amount,
                        AVG(tip_amount) as average_tip_amount,
                        PULocationID
                    FROM `{table_uri}`
                    GROUP BY PULocationID""",
    con=db,
)
trip_count max_total_amount average_tip_amount PULocationID
0 1 3.80 0.000000 7
1 4 6.30 0.002500 193
2 3 11.27 1.276667 238
3 1 12.30 1.500000 239
4 1 14.15 2.350000 246

Clean up

Clean up in the end by deleting the table.

# Clean up
if os.path.exists(table_uri):
    shutil.rmtree(table_uri)
Introduction
Foundation