Skip to content

Fields

API Documentation

typespark.field
typespark.decimal

Field specifiers

field() and decimal() set per-column options. To pin decimal precision and scale on a schema field (so generate_schema() emits the right DecimalType), use decimal():

class Invoice(ts.DataFrame):
    invoice_id: ts.String = ts.field(nullable=False)
    total: ts.Decimal = ts.decimal(12, 2)  # -> DecimalType(12, 2)
    note: ts.String = ts.field(df_alias="invoice_note")

Both accept df_alias= (see Aliases), nullable= and any attrs.field argument (converter=, default=, factory=, ...).

Nullability

Columns are nullable by default. nullable=False sets nullable on the generated StructField. It is schema metadata only; null values are not checked at runtime.

Converters

Transform values with converter=:

import typespark as ts


class T(ts.DataFrame):
    n: ts.Integer = ts.field(
        converter=lambda x: x + 1, df_alias="raw_n"
    )

Converters follow attrs: they run on values passed to the constructor and on defaults. from_df() takes columns it reads as already converted and does not run the converter on them (it re-wraps after every operation, so it would apply again).