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
DataSetsbased on their schemasImproved documentation of tables
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
FAQ
- Typedspark: column-wise type annotations for pyspark DataFrames
- In your IDE
- In your Notebooks
- Advanced Topics
- Contributing
- API Documentation
ArrayTypeCatalogsColumnColumnMetaDataSetDataSet.alias()DataSet.cache()DataSet.checkpoint()DataSet.coalesce()DataSet.distinct()DataSet.dropDuplicates()DataSet.dropDuplicatesWithinWatermark()DataSet.drop_duplicates()DataSet.dropna()DataSet.exceptAll()DataSet.fillna()DataSet.filter()DataSet.hint()DataSet.intersect()DataSet.intersectAll()DataSet.join()DataSet.limit()DataSet.localCheckpoint()DataSet.observe()DataSet.orderBy()DataSet.persist()DataSet.repartition()DataSet.repartitionByRange()DataSet.replace()DataSet.sample()DataSet.sampleBy()DataSet.sort()DataSet.sortWithinPartitions()DataSet.subtract()DataSet.transform()DataSet.union()DataSet.unionAll()DataSet.unionByName()DataSet.unpersist()DataSet.where()
DataSetImplementsDataSetImplements.alias()DataSetImplements.cache()DataSetImplements.checkpoint()DataSetImplements.coalesce()DataSetImplements.distinct()DataSetImplements.dropDuplicates()DataSetImplements.dropDuplicatesWithinWatermark()DataSetImplements.drop_duplicates()DataSetImplements.dropna()DataSetImplements.exceptAll()DataSetImplements.fillna()DataSetImplements.filter()DataSetImplements.hint()DataSetImplements.intersect()DataSetImplements.intersectAll()DataSetImplements.join()DataSetImplements.limit()DataSetImplements.localCheckpoint()DataSetImplements.observe()DataSetImplements.orderBy()DataSetImplements.persist()DataSetImplements.repartition()DataSetImplements.repartitionByRange()DataSetImplements.replace()DataSetImplements.sample()DataSetImplements.sampleBy()DataSetImplements.sort()DataSetImplements.sortWithinPartitions()DataSetImplements.subtract()DataSetImplements.transform()DataSetImplements.typedspark_schemaDataSetImplements.union()DataSetImplements.unionAll()DataSetImplements.unionByName()DataSetImplements.unpersist()DataSetImplements.where()
DatabaseDatabasesDayTimeIntervalTypeDecimalTypeIntervalTypeMapTypeMetaSchemaMetaSchema.all_column_names()MetaSchema.all_column_names_except_for()MetaSchema.get_dlt_kwargs()MetaSchema.get_docstring()MetaSchema.get_metadata()MetaSchema.get_schema_definition_as_string()MetaSchema.get_schema_name()MetaSchema.get_snake_case()MetaSchema.get_structtype()MetaSchema.print_schema()
SchemaStructTypecreate_empty_dataset()create_partially_filled_dataset()create_schema()create_structtype_row()load_table()register_schema_to_dataset()register_schema_to_dataset_with_alias()structtype_column()transform_to_schema()