Columns¶
API Documentation
A typed column wraps a pyspark.sql.Column and records its Spark type in its Python
class: ts.String holds a string column, ts.Int an integer column. On a typed DataFrame, each
model attribute is a typed column.
Operations on typed columns return typed columns, so the type follows the expression:
age = person.age # Int
next_age = person.age + 1 # Int
adult = person.age >= 18 # Bool
starts_with_a = person.name.like("A%") # Bool
Column types¶
| Type | Spark type |
|---|---|
ts.String |
StringType |
ts.Bool |
BooleanType |
ts.Binary |
BinaryType |
ts.Short |
ShortType |
ts.Integer (alias ts.Int) |
IntegerType |
ts.Long |
LongType |
ts.Float |
FloatType |
ts.Double |
DoubleType |
ts.Decimal |
DecimalType (precision and scale set with ts.decimal) |
ts.Date |
DateType |
ts.Timestamp |
TimestampType |
ts.TimestampNTZ |
TimestampNTZType |
ts.Array[T] |
ArrayType of T (see Nested data) |
a ts.Struct subclass |
StructType (see Nested data) |
ts.StringOf[Literal[...]], ts.IntOf[Literal[...]] |
StringType, IntegerType (see Enums) |
Wrap and unwrap¶
wrap() wraps a Spark column and keeps Spark's type; calling the type (ts.String(col))
wraps and casts it. to_spark() gives the Spark column back:
# wrap, keep Spark's type
upper = ts.String.wrap(F.upper(F.col("name")))
as_text = ts.String(F.col("age")) # wrap and cast to string
# unwrap: the plain pyspark Column
spark_col = upper.to_spark()
Use to_spark() to pass a column to PySpark code that TypeSpark doesn't cover.
Custom column types¶
Column types are classes too. Subclass one to give a column domain behaviour; the Spark type stays the parent's:
import typespark.functions as tsf
class Email(ts.String):
def domain(self) -> ts.String:
return tsf.substring_index(self, "@", -1)
class Contact(ts.DataFrame):
name: ts.String
email: Email
contacts.filter(contacts.email.domain() == "example.com")
Casting¶
Calling a type constructs (and casts) a typed column from a literal or expression:
ts.Int(df.raw_count) # cast an existing column to Integer
ts.Decimal(3.14) # DecimalType(10, 0) by default
ts.Decimal(3.14, 12, 4) # DecimalType(precision=12, scale=4)
Enums¶
StringOf[T] and IntOf[T] restrict a column to a fixed set of Literal values.
Comparisons and isin() only accept those values, so a typo is a type error:
from typing import Literal
import typespark as ts
class Ticket(ts.DataFrame):
status: ts.StringOf[Literal["open", "closed"]]
priority: ts.IntOf[Literal[1, 2, 3]]
tickets = Ticket.from_df(raw_df)
tickets.filter(tickets.status == "open") # ok
tickets.filter(tickets.priority.isin(1, 3)) # ok
tickets.filter(tickets.status == "opne") # type error
tickets.filter(tickets.priority.isin(1, 4)) # type error
Comparing with a plain String / Integer column is also a type error, since a
plain column may hold any value. To compare anyway, wrap the plain column to
vouch for its values:
tickets.filter(tickets.priority == raw.priority_code) # type error
vouched = ts.IntOf[Literal[1, 2, 3]](raw.priority_code)
tickets.filter(tickets.priority == vouched) # ok
The schema is plain StringType / IntegerType. Values are not validated at
runtime. Arithmetic on an IntOf returns a plain Integer, since the result can
fall outside the allowed values.
Literal types¶
Function arguments that only take a fixed set of strings (units, formats, days, join
types) are Literal types too: see Units, formats and days.