Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 9

Multi-Format Data Ingestion (Parquet, Arrow, CSV & SQL)

Production machine learning pipelines ingest data from heterogeneous backends: data lakes (Apache Parquet, Delta Lake), relational warehouses (PostgreSQL, Snowflake via SQLAlchemy), and streaming APIs (JSON/REST). CSV files are text-based, lack schema enforcement, and require expensive type inference. Parquet is columnar, compressed (Snappy/ZSTD), and embeds column metadata, making it 10-50x faster to read in modern ML pipelines.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Benchmarking Parquet vs CSV & Database Query Ingestion

Benchmarking Parquet vs CSV & Database Query Ingestion
import pandas as pd
import numpy as np
import tempfile
import os
import time

# Create benchmark dataset (100,000 records)
n = 100_000
df = pd.DataFrame({
    'timestamp': pd.date_range('2026-01-01', periods=n, freq='min'),
    'device_id': np.random.randint(1000, 9999, size=n),
    'sensor_reading': np.random.randn(n),
    'status_code': np.random.choice(['OK', 'WARNING', 'CRITICAL'], size=n)
})

with tempfile.TemporaryDirectory() as tmpdir:
    csv_path = os.path.join(tmpdir, 'telemetry.csv')
    parquet_path = os.path.join(tmpdir, 'telemetry.parquet')

    # 1. WRITE BENCHMARK
    t0 = time.perf_counter()
    df.to_csv(csv_path, index=False)
    t_csv_w = time.perf_counter() - t0

    t0 = time.perf_counter()
    df.to_parquet(parquet_path, engine='pyarrow', compression='snappy')
    t_parquet_w = time.perf_counter() - t0

    # 2. READ BENCHMARK
    t0 = time.perf_counter()
    df_from_csv = pd.read_csv(csv_path)
    t_csv_r = time.perf_counter() - t0

    t0 = time.perf_counter()
    # Parquet allows Column Pruning: read ONLY columns needed for ML modeling
    df_from_pq = pd.read_parquet(parquet_path, columns=['sensor_reading', 'status_code'])
    t_parquet_r = time.perf_counter() - t0

    size_csv_mb = os.path.getsize(csv_path) / (1024 ** 2)
    size_pq_mb = os.path.getsize(parquet_path) / (1024 ** 2)

    print(f"File Storage Size : CSV = {size_csv_mb:.2f} MB | Parquet = {size_pq_mb:.2f} MB ({size_csv_mb/size_pq_mb:.1f}x compression)")
    print(f"Write Performance : CSV = {t_csv_w*1000:.1f} ms | Parquet = {t_parquet_w*1000:.1f} ms")
    print(f"Read Performance  : CSV = {t_csv_r*1000:.1f} ms | Parquet = {t_parquet_r*1000:.1f} ms ({t_csv_r/t_parquet_r:.1f}x faster!)")

Pro Tip: Predicate Pushdown with Parquet

When reading multi-gigabyte Parquet datasets from S3/GCS, use predicate pushdown: `pd.read_parquet('s3://lake/events.parquet', filters=[('year', '==', 2026), ('country', '==', 'US')])`. The storage engine inspects Parquet chunk-level min/max statistics to skip reading unneeded row groups from disk entirely.

Diagram
Loading diagram…

MlLoadingSavingData Visual Representation

Hands-On Challenge: Chunked CSV Ingestion Pipeline

Write a generator function `read_massive_csv_chunks(filepath, chunksize=50000)` that streams a large CSV file in chunks, computes rolling aggregations for a target column, and yields a compact summary DataFrame without exceeding 200MB of RAM.

Quick Quiz & Key Concept Checks

Common Pitfall: Unsanitized SQL Ingestion in ML Pipelines

Using Python f-strings to format SQL queries (`pd.read_sql(f'SELECT * FROM tx WHERE id = {user_input}')`) exposes your data pipeline to critical SQL injection attacks. Always use parameterized queries via SQLAlchemy text binding: `pd.read_sql(text('SELECT * FROM tx WHERE id = :id'), con=engine, params={'id': user_input})`.

Topics in This Module

0/9 done