Skip to content

Migrating from 1.x

Every breaking change in 2.0, with what to change.

1.x 2.0
ts.TypedColumn, ts.BaseDataFrame, ts.TypedArrayType, ts.ArrayOf(T) ts.Column, ts.DataFrame, ts.Array, ts.Array[T]
df.arr.explode().id df.arr.explode(lambda x: x.id)
select(df.arr.explode()) select(df.arr.explode().alias("name"))
ts.field(default=<column>) ts.field(factory=lambda: <column>)
ts.field(default=attrs.Factory(...)) ts.field(factory=attrs.Factory(...))
ts.string_literal(v), ts.int_literal(v), ts.bool_literal(v) ts.String(v), ts.Integer(v), ts.Bool(v)
tsf.date_trunc("day", ...), tsf.next_day(..., "Monday") tsf.date_trunc("DAY", ...), tsf.next_day(..., "MONDAY")
typespark CLI, typespark.data_contract, typespark[datacontract] removed
_set_name(), _set_column(), AliasedTypedColumn, UnnamedColumnError removed

Renamed classes

Column, DataFrame and Array are now the class names instead of aliases, and the old names are removed. Array[T] is the only public way to get an array column class; ArrayOf(T) is gone.

# 1.x
class Building(ts.BaseDataFrame):
    areas: ts.TypedArrayType[Area]

# 2.0
class Building(ts.DataFrame):
    areas: ts.Array[Area]

Class names in reprs and error messages change accordingly.

explode(access)

explode() now returns the element as a real typed column, and explode(access) returns whatever access builds from it. Reading a field off the exploded element moves into access:

# 1.x
area_id=df.areas.explode().id

# 2.0
area_id=df.areas.explode(lambda a: a.id)

df.areas.explode().id now raises ExplodeAccessError, as does exploding two arrays in one DataFrame. Several explode() calls on the same array share one explode. See Generators.

arr.explode(...) inside withColumn raises ExplodeAccessError too: withColumn never runs the explode. Use tsf.explode(arr) there, or build the column through the model's constructor.

Columns no longer carry names

Columns don't have an internal name any more, so:

  • _set_name(), _set_column(), AliasedTypedColumn and UnnamedColumnError are removed. alias() keeps the column's class.
  • select(df.arr.explode()) needs an explicit .alias(...).
  • dropDuplicates(subset) and withWatermark only accept columns of a typed DataFrame, and raise on computed columns.

Column defaults go in factory=

default= takes a literal or None. A column expression as default= raises TypeError: it was built at class definition (needing a Spark session at import) and shared by every instance. Use factory=:

# 1.x
created: ts.Date = ts.field(default=ts.Date(F.current_date()))
next_age: ts.Int = ts.field(default=attrs.Factory(lambda self: self.age + 1, takes_self=True))

# 2.0
created: ts.Date = ts.field(factory=lambda: ts.Date(F.current_date()))
next_age: ts.Int = ts.field(factory=attrs.Factory(lambda self: self.age + 1, takes_self=True))

default=attrs.Factory(...) still runs, but no longer type checks. The same applies to ts.decimal(...). See Defaults.

Literal helpers removed

ts.string_literal, ts.int_literal and ts.bool_literal were deprecated in 1.x and are removed. Call the type instead: ts.String("x"), ts.Integer(1), ts.Bool(True).

Units, formats, days and join types are Literal types

Arguments Spark only accepts from a fixed set are now Literal types, so a typo is a type error instead of a failure when the query runs:

Argument Type
tsf.timestamp_diff unit tsf.TimeUnit
tsf.timestamp_add unit tsf.TimestampAddUnit
tsf.date_trunc format tsf.DateTruncFormat
tsf.trunc format tsf.TruncFormat
tsf.next_day day tsf.DayOfWeek
DataFrame.join how ts.JoinType

Units, formats and days are uppercase, as Spark documents them: date_trunc("day", ...) and next_day(..., "Monday") are type errors, use "DAY" and "MONDAY". Join types are lowercase and accept every spelling Spark does (leftouter, left_outer, ...). Runtime behaviour is unchanged.

Data contract CLI removed

The typespark command, typespark.data_contract and the typespark[datacontract] extra are removed. typespark no longer depends on pyyaml. To get a schema from a model, use generate_schema().