Skip to main content

Column-wise type annotations for pyspark DataFrames

Project description

Typedspark: column-wise type annotations for pyspark DataFrames

We love Spark! But in production code we're wary when we see:

from pyspark.sql import DataFrame

def foo(df: DataFrame) -> DataFrame:
    # do stuff
    return df

Because… How do we know which columns are supposed to be in df?

Using typedspark, we can be more explicit about what these data should look like.

from typedspark import Column, DataSet, Schema
from pyspark.sql.types import LongType, StringType

class Person(Schema):
    id: Column[LongType]
    name: Column[StringType]
    age: Column[LongType]

def foo(df: DataSet[Person]) -> DataSet[Person]:
    # do stuff
    return df

The advantages include:

  • Improved readability of the code
  • Typechecking, both during runtime and linting
  • Auto-complete of column names
  • Easy refactoring of column names
  • Easier unit testing through the generation of empty DataSets based on their schemas
  • Improved documentation of tables

Documentation

Please see our documentation on readthedocs.

Installation

You can install typedspark from pypi by running:

pip install typedspark

By default, typedspark does not list pyspark as a dependency, since many platforms (e.g. Databricks) come with pyspark preinstalled. If you want to install typedspark with pyspark, you can run:

pip install "typedspark[pyspark]"

Compatibility

Typedspark is tested in CI with PySpark 3.5.7 and 4.1.0. Spark Connect is supported when using PySpark 4.x, and the Connect-specific test runs if SPARK_CONNECT_URL is set.

Demo videos

IDE demo

https://github.com/kaiko-ai/typedspark/assets/47976799/e6f7fa9c-6d14-4f68-baba-fe3c22f75b67

You can find the corresponding code here.

Jupyter / Databricks notebooks demo

https://github.com/kaiko-ai/typedspark/assets/47976799/39e157c3-6db0-436a-9e72-44b2062df808

You can find the corresponding code here.

FAQ

I found a bug! What should I do?
Great! Please make an issue and we'll look into it.

I have a great idea to improve typedspark! How can we make this work?
Awesome, please make an issue and let us know!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typedspark-1.6.3.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typedspark-1.6.3-py3-none-any.whl (38.1 kB view details)

Uploaded Python 3

File details

Details for the file typedspark-1.6.3.tar.gz.

File metadata

  • Download URL: typedspark-1.6.3.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for typedspark-1.6.3.tar.gz
Algorithm Hash digest
SHA256 443ef513a86e5f5224615a975a3682e002130f3ac13842d30efcd472e93cb39c
MD5 bc40a313302c11deb8a6d6c771b54fbd
BLAKE2b-256 d16b0bc280dccdc5e276fef51b6d59a714b98db4705cf065f0855763f0ff705f

See more details on using hashes here.

File details

Details for the file typedspark-1.6.3-py3-none-any.whl.

File metadata

  • Download URL: typedspark-1.6.3-py3-none-any.whl
  • Upload date:
  • Size: 38.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for typedspark-1.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cef8f7aff3c53b0c7b2a434a7bc1de6114744a73a6b8d3c95f5517dd9b248319
MD5 785f0c2cc6c2406135269594f7db4569
BLAKE2b-256 131348868c1c84ce9dcc2c3b0a78673a9d988f33fb6d59f8bc19736451f5352f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page