Strings, dates and timestamps, nulls, and complex types (arrays, maps, structs), plus regex and type casting.
⏱ 5 min readTopics chapter readerLevel · Structured APIs
01 · Overview
Start here
⏱ 4 min · Topic 1 of 6
A column has one type: text, number, date, or a nested type such as a list. This chapter shows the functions for each type.
All of them are in pyspark.sql.functions, imported as F. They run inside Spark, on the executors. Chapter 14, UDFs & the Pandas API on Spark, shows why that is faster than your own Python function.
The examples use the shop: orders.csv from chapter 3, the customers, and the order items.
02 · Text
Strings
⏱ 6 min · Topic 2 of 6
A partner app sends customer names with extra spaces and mixed case. This made-up data shows the cleaning functions:
[CANNOT_PARSE_TIMESTAMP] Text '2026-01-03' could not be parsed at index 2.
The second text is not day/month/year. Use try_to_date to get NULL instead of an error.
With F.try_to_date in the same code, the first row gives 2026-01-03 and the second gives NULL.
Timestamps also depend on the session time zone, the setting spark.sql.session.timeZone. Set it on purpose, for example to UTC, so a day starts at the same moment on every machine.
04 · NULL
Nulls: the silent bug
⏱ 6 min · Topic 4 of 6
NULL means a missing value. Most functions skip it without a warning. That changes counts and averages:
The print shows 5: dropna removed order 105. A 0.0 now pulls the average down, so choose on purpose.
You cannot find NULL with ==. The test col("country") == None returns no rows at all. Use isNull(), or eqNullSafe, which treats two NULL values as equal: