Chapter 9: DataFrames, columns and schemas¶
Part II used DataFrames as a convenient way to write a query. This chapter looks at what a DataFrame is. A DataFrame holds no data. It holds a schema and a plan, and each method that you call adds one node to that plan. The schema is the contract between your code and the data, and data from other teams often breaks that contract.
A DataFrame is an immutable pair of a schema and a logical plan. The analyzer resolves names, types and nullability in that plan before any task runs. Thus the shape of your code changes the work of the analyzer. The schema that you declare decides what the data can do to your job.
The scripts are in code/ch09, and each output block below comes from code/ch09/output/. The scripts use two data sets:
plans.pyandtype_system.pyuse the June 2026 taxi trips from Part II.drift.pyuses three days of order events from SparkTutor, copied tocode/data/events. The shape of these events changes every day.
Step 1: A DataFrame is a schema and a plan¶
The first lines of plans.py read the trips and select three columns:
trips = spark.read.parquet(str(TRIPS))
fares = trips.select("fare_amount", "tip_amount", "tolls_amount")
print(fares.schema.simpleString())
print("jobs run by read + select:", [(j["name"], j["numTasks"]) for j in new_jobs])
print("same object after withColumn?", fares is fares.withColumn("x", F.lit(1)))
== 1. a DataFrame is a schema and a plan
struct<fare_amount:double,tip_amount:double,tolls_amount:double>
jobs run by read + select: [('parquet at NativeMethodAccessorImpl.java:0', 1)]
same object after withColumn? False
The schema is known before Spark reads a row. Parquet keeps its schema in the file footer, and spark.read.parquet runs one job with one task to read that footer.
In the 4.3.0 source, ParquetUtils.inferSchema sends the files to ParquetFileFormat.mergeSchemasInParallel, and that function starts the job. Without mergeSchema, only one file is in that job. The select runs nothing. It returns a new DataFrame that wraps the old plan in a Project node.
A DataFrame is immutable. withColumn does not change fares. It returns a different object with a larger plan. This is not a copy of data, because there is no data to copy.
Immutability is important for a different reason. A plan that never changes is a full recipe for its result. Thus Spark can run the plan again for a lost partition, and it can compare two plans, as Chapter 7 did with sameSemantics.
Step 2: The everyday operations¶
Most DataFrame code uses a small set of methods. basics.py shows them on the "blogs" table from the book Learning Spark. It is six rows with an explicit schema:
schema = StructType([
StructField("Id", IntegerType(), False),
StructField("First", StringType()),
StructField("Last", StringType()),
StructField("Url", StringType()),
StructField("Published", StringType()),
StructField("Hits", IntegerType()),
StructField("Campaigns", ArrayType(StringType())),
])
blogs = spark.createDataFrame(data, schema)
== 1. the blogs table, with an explicit schema
root
|-- Id: integer (nullable = false)
|-- First: string (nullable = true)
|-- Last: string (nullable = true)
|-- Url: string (nullable = true)
|-- Published: string (nullable = true)
|-- Hits: integer (nullable = true)
|-- Campaigns: array (nullable = true)
| |-- element: string (containsNull = true)
The schema says that Id cannot be NULL, and that Campaigns is an array of strings. createDataFrame makes sure that each Python row agrees with this schema (Step 11 shows the check). Then a chain of methods makes a report:
report = (blogs
.where(F.col("Hits") > 5000)
.withColumns({"Big_Hitter": F.col("Hits") > 10000,
"Published_Date": F.to_date("Published", "M/d/yyyy"),
"Channels": F.size("Campaigns")})
.drop("Url", "Published", "Campaigns")
.withColumnRenamed("First", "first_name")
.orderBy(F.desc("Hits")))
== 2. select, where, withColumns, drop, rename, sort
+---+----------+-------+-----+----------+--------------+--------+
| Id|first_name| Last| Hits|Big_Hitter|Published_Date|Channels|
+---+----------+-------+-----+----------+--------------+--------+
| 5| Matei|Zaharia|40578| true| 2014-05-14| 4|
| 6| Reynold| Xin|25568| true| 2015-03-02| 2|
| 4| Tathagata| Das|10568| true| 2018-05-12| 2|
| 2| Brooke| Wenig| 8908| false| 2018-05-05| 2|
| 3| Denny| Lee| 7659| false| 2019-06-07| 4|
+---+----------+-------+-----+----------+--------------+--------+
columns: ['Id', 'first_name', 'Last', 'Hits', 'Big_Hitter', 'Published_Date', 'Channels']
drop a name that is not there: True
Each method returns a new DataFrame:
wherekeeps the rows where the condition is true.filteris the same method with a different name.withColumnsadds or replaces columns from a dictionary.to_datewith the patternM/d/yyyychanges the text5/14/2014into adate.sizecounts the elements of an array.dropremoves columns. A name that is not in the DataFrame gives no error, so a misspelt name indropkeeps the column.withColumnRenamedchanges one name.withColumnsRenamed(Step 16) changes many names with a dictionary.orderBysorts.F.descsorts from the largest value to the smallest.
You can also write an expression as SQL text. selectExpr takes one SQL expression for each column:
tiers = blogs.selectExpr(
"Id",
"CASE WHEN Hits > 20000 THEN 'star' WHEN Hits > 8000 THEN 'regular' ELSE 'new' END AS tier",
"Hits::STRING || ' hits' AS label",
"array_contains(Campaigns, 'FB') AS on_fb")
== 3. expr, selectExpr and SQL expressions
+---+-------+----------+-----+
| Id| tier| label|on_fb|
+---+-------+----------+-----+
| 1| new| 4535 hits|false|
| 2|regular| 8908 hits|false|
| 3| new| 7659 hits| true|
| 4|regular|10568 hits| true|
| 5| star|40578 hits| true|
| 6| star|25568 hits|false|
+---+-------+----------+-----+
expr vs when/otherwise, same optimized plan: True
CASE WHEN in SQL and F.when(...).when(...).otherwise(...) in Python make the same expression. The last line of the output compares the two optimized plans, and they are the same. The first condition that is true gives the value. A row that matches no condition and has no ELSE (or no otherwise) gets NULL. Hits::STRING is the cast syntax that Spark 4.0 added, and || joins two strings.
Choose the form that is easier to read. A long CASE WHEN is often clearer as SQL text. A condition that your program builds in a loop is easier as Python Column objects. An error in SQL text shows only when the driver parses it. But that also occurs before any task runs.
Step 3: Three ways to name a column¶
A Column is an expression, not a set of values. You can make one in three ways:
print(repr(F.col("fare_amount")), repr(fares["fare_amount"]), repr(F.expr("fare_amount * 2")))
print("F.col('no_such_column') ->", repr(F.col("no_such_column"))) # a name, not checked yet
try:
fares["no_such_column"] # Classic checks it now
except Exception as e:
print("fares['no_such_column'] ->", str(e).splitlines()[0][:70])
== 2. column references
Column<'fare_amount'> Column<'fare_amount'> Column<'fare_amount * 2'>
F.col('no_such_column') -> Column<'no_such_column'>
fares['no_such_column'] -> [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or function pa
The three forms are different:
F.col("x")makes an unresolved attribute. It is only a name. Spark examines it later, when the column is part of a DataFrame.df["x"]asks the DataFrame for its column. In Classic PySpark, the driver resolves the name immediately, so a bad name fails on that line.F.expr("...")sends a SQL string to the SQL parser. Use it for SQL syntax that has no Python function, for exampleTRIM(LEADING '0' FROM x).
On Spark Connect, df["x"] does not fail immediately. basics.py runs the same two lines in Classic mode and against the Part III Connect server:
== 7. a bad column name, mode: Classic
df['typo']: ERROR [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or function parameter with n
df.select(df['typo']).collect(): ERROR [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or function parameter with n
== 7. a bad column name, mode: Connect
df['typo']: Column<'typo'>
df.select(df['typo']).collect(): ERROR [CANNOT_RESOLVE_DATAFRAME_COLUMN] Cannot resolve dataframe column "typo". It's proba
In Classic mode, df["typo"] fails on its line. On Connect, it returns a Column and no error occurs. The Connect client does not keep the schema. It puts the name in the plan, and the server finds the error when it analyzes the plan, at collect(). The error class is also different. Chapter 24 showed this difference in more detail. Code that must run in the two modes cannot use the df["x"] line as a check.
Step 4: What a withColumn chain adds to the plan¶
plans.py adds three columns with three withColumn calls. Then it prints three of the plans that Catalyst makes from the chain:
chain = (fares
.withColumn("tip_pct", F.col("tip_amount") / F.col("fare_amount"))
.withColumn("has_toll", F.col("tolls_amount") > 0)
.withColumn("fare_band", F.when(F.col("fare_amount") < 10, "short").otherwise("long")))
qe = chain._jdf.queryExecution()
flowchart LR
classDef you fill:#3a5f96,fill-opacity:0.72,stroke:#3a5f96,color:#ffffff
classDef jvm fill:#c0661a,fill-opacity:0.72,stroke:#c0661a,color:#ffffff
code["DataFrame calls<br/>or SQL text"]:::you --> parsed["parsed plan<br/>names not resolved"]:::jvm
parsed -->|"analyzer: names, types,<br/>nullability"| analyzed["analyzed plan"]:::jvm
analyzed -->|"optimizer: rules<br/>such as CollapseProject"| optimized["optimized plan"]:::jvm
optimized -->|"planner"| physical["physical plan"]:::jvm
The relation at the bottom of each plan has all 21 columns of the file. The text below shows these columns only in the first plan, because they do not change:
== 3. withColumn x3: parsed (unresolved) plan
'Project [unresolvedstarwithcolumns(fare_band, CASE WHEN '`<`('fare_amount, 10) THEN short ELSE long END, None)]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14, tip_pct#23, (tolls_amount#14 > cast(0 as double)) AS has_toll#24]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14, (tip_amount#13 / fare_amount#10) AS tip_pct#23]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14]
+- Relation [VendorID#0,tpep_pickup_datetime#1,...,request_source#20] parquet
== analyzed plan
Project [fare_amount#10, tip_amount#13, tolls_amount#14, tip_pct#23, has_toll#24, CASE WHEN (fare_amount#10 < cast(10 as double)) THEN short ELSE long END AS fare_band#25]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14, tip_pct#23, (tolls_amount#14 > cast(0 as double)) AS has_toll#24]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14, (tip_amount#13 / fare_amount#10) AS tip_pct#23]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14]
+- Relation [...] parquet
== optimized plan
Project [fare_amount#10, tip_amount#13, tolls_amount#14, (tip_amount#13 / fare_amount#10) AS tip_pct#23, (tolls_amount#14 > 0.0) AS has_toll#24, CASE WHEN (fare_amount#10 < 10.0) THEN short ELSE long END AS fare_band#25]
+- Relation [...] parquet
Read the plans from the bottom to the top. Four facts are in them:
- The parsed plan is almost resolved. Only the top node has a quote mark (
'Project), which means "not resolved". In Classic PySpark, eachwithColumncall makes a new DataFrame, and the driver analyzes each new DataFrame immediately. Thus the lower nodes already have attribute IDs such asfare_amount#10. - An attribute ID is the true name of a column.
#10is thefare_amountfrom the file in every node. The analyzer uses these IDs, not the text names, after resolution. - The analyzer adds casts.
fare_amount < 10compares a double with an integer literal. The analyzed plan showscast(10 as double). The optimizer then folds the cast into the constant10.0. - Each
withColumnadds oneProject. Three calls give three newProjectnodes on top of theselect. The optimizer ruleCollapseProjectmerges them into one node, because two projections, one on top of the other, are one projection.
withColumns takes a dictionary and makes one Project for all the new columns:
== withColumns: analyzed plan
Project [fare_amount#10, tip_amount#13, tolls_amount#14, (tip_amount#13 / fare_amount#10) AS tip_pct#26, (tolls_amount#14 > cast(0 as double)) AS has_toll#27, CASE WHEN (fare_amount#10 < cast(10 as double)) THEN short ELSE long END AS fare_band#28]
+- Project [fare_amount#10, tip_amount#13, tolls_amount#14]
+- Relation [...] parquet
same optimized plan: True
The two optimized plans are the same, so the two forms run the same tasks. The difference is in the analyzed plan, which is the plan that the driver must build and examine. The next step measures that difference.
flowchart BT
classDef node fill:#3a5f96,fill-opacity:0.72,stroke:#3a5f96,color:#ffffff
classDef opt fill:#c0661a,fill-opacity:0.72,stroke:#c0661a,color:#ffffff
subgraph A["analyzed: withColumn x3"]
direction BT
r1["Relation"]:::node --> p0["Project: select"]:::node --> p1["Project: + tip_pct"]:::node --> p2["Project: + has_toll"]:::node --> p3["Project: + fare_band"]:::node
end
subgraph B["analyzed: withColumns"]
direction BT
r2["Relation"]:::node --> q0["Project: select"]:::node --> q1["Project: + 3 columns"]:::node
end
subgraph C["optimized: both"]
direction BT
r3["Relation"]:::opt --> o1["one Project"]:::opt
end
Step 5: The cost of a long chain¶
Many programs call withColumn in a loop, once for each new column. Each call makes a new DataFrame, and the driver analyzes each one. Thus the work of the analyzer increases with each call. plans.py builds DataFrames with n new columns in two ways, and measures the time to build, analyze and optimize each one:
def build_loop(n):
df = fares
for i in range(n):
df = df.withColumn(f"c{i}", F.col("fare_amount") + i)
return df
def build_once(n):
return fares.withColumns({f"c{i}": F.col("fare_amount") + i for i in range(n)})
== 4. build + analyze + optimize, median of 7 after a warmup (ms)
columns withColumn loop withColumns Projects (loop)
25 48.9 25.3 26
50 101.1 47.4 51
100 286.2 110.7 101
200 957.4 285.9 201
400 7582.4 703.5 401
The script ran under the shared bench lock on a quiet machine, with local[*] on 16 cores. Each number is the median of 7 runs after one warmup run. The loop leaves one Project for each call (plus the select), and withColumns leaves one.
n new columns, from the table above. At 400 columns, the loop takes 7,582.4 ms and withColumns takes 703.5 ms.The two curves do not have the same shape. From 200 to 400 columns, withColumns takes about 2.5 times longer. The loop takes about 8 times longer. Each loop call analyzes a plan that is one node deeper than the plan before it. Thus the total work increases faster than the number of columns.
No task runs in this measurement. All the time is driver time, before the first job starts. The loop also leaves one Project node per call in the analyzed plan, and the optimizer must collapse all of them.
withColumn call adds a Project, and the driver analyzes the new plan. withColumns adds one Project. The optimizer merges both into one. The animation shows six calls. The times are from the 400-column run.With a few columns the difference is small. With hundreds of columns, the loop costs seconds for each DataFrame that you build. If you build such a DataFrame inside another loop, the cost multiplies. Use withColumns or one select when you add more than a few columns.
Step 6: Spark's types¶
typeof() gives the SQL type of any value. type_system.py applies it to a literal of each main type:
== 1. typeof over literals
true boolean
1Y tinyint
1S smallint
1 int
1L bigint
1.5F float
1.5D double
1.5 decimal(2,1)
'x' string
X'00' binary
DATE'2026-06-01' date
TIMESTAMP'2026-06-01 10:00:00' timestamp
TIMESTAMP_NTZ'2026-06-01 10:00:00' timestamp_ntz
INTERVAL 2 DAYS interval day
INTERVAL '1-2' YEAR TO MONTH interval year to month
array(1, 2) array<int>
map('a', 1) map<string,int>
named_struct('id', 1, 'plan', 'pro') struct<id:int,plan:string>
parse_json('{"a": 1}') variant
'x' COLLATE UTF8_LCASE string collate UTF8_LCASE
TIME'10:07:00' time(6)
A literal with a decimal point and no suffix is a decimal, not a double. Thus 1.5 is decimal(2,1). The table gives the DDL name, the PySpark class and the Python value for each type:
| DDL name | PySpark class | Holds | Python value |
|---|---|---|---|
BOOLEAN |
BooleanType |
true or false | bool |
TINYINT, SMALLINT |
ByteType, ShortType |
1-byte and 2-byte integers | int |
INT |
IntegerType |
4-byte integers | int |
BIGINT |
LongType |
8-byte integers | int |
FLOAT, DOUBLE |
FloatType, DoubleType |
binary floating point | float |
DECIMAL(p,s) |
DecimalType(p, s) |
exact decimals, p digits, s after the point |
decimal.Decimal |
STRING |
StringType |
text, with an optional collation | str |
BINARY |
BinaryType |
bytes | bytes, bytearray |
DATE |
DateType |
a calendar day | datetime.date |
TIMESTAMP |
TimestampType |
an instant | datetime.datetime |
TIMESTAMP_NTZ |
TimestampNTZType |
a wall-clock date and time | datetime.datetime |
TIME(p) |
TimeType |
a time of day (Step 8) | datetime.time |
INTERVAL ... |
DayTimeIntervalType, YearMonthIntervalType |
a length of time | datetime.timedelta (day-time only) |
ARRAY<T> |
ArrayType(T) |
an ordered list | list |
MAP<K,V> |
MapType(K, V) |
key to value pairs | dict |
STRUCT<...> |
StructType([...]) |
named fields | Row |
VARIANT |
VariantType |
any JSON-like value | VariantVal |
VARIANT holds a JSON-like value as one typed column. Spark keeps it in a binary form, so a query can get a field without a parse of the full text for each row. basics.py gives a first look:
v.selectExpr("typeof(payload)", "variant_get(payload, '$.device.os', 'string') AS os",
"payload:device.ver::int AS ver", "payload:tags[1]::string AS tag2")
== 6. VARIANT
+---------------+---+---+----+
|typeof(payload)| os|ver|tag2|
+---------------+---+---+----+
| variant|ios| 17| b|
+---------------+---+---+----+
variant_get takes a JSON path and a target type. The : operator is a short form of the same path, and :: casts the result. Part VI gives VARIANT its own chapter, with the 4.3 functions that edit a value. Chapter 10 shows the collated string type. Spark 4.3 also has a preview of nanosecond timestamps, and Chapter 10 shows them.
Step 7: Numbers: overflow, doubles and decimals¶
ANSI mode is on by default since Spark 4.0. Thus a number that does not fit its type causes an error. It does not wrap to a different value:
== 2. numbers
SELECT CAST(300 AS TINYINT) ERROR [CAST_OVERFLOW] The value 300 of the type "INT" cannot be cast to "TINYINT" due to an overflow.
SELECT 2147483647 + 1 ERROR [ARITHMETIC_OVERFLOW] overflow. Use 'try_add' to tolerate overflow and return NULL instead. If n
SELECT CAST(12345.678 AS DECIMAL(5,2)) ERROR [NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION] 12345.678 cannot be represented as Decimal(5, 2).
Row(dbl=0.30000000000000004, dec=Decimal('0.3'), rounded=Decimal('123.46'), too_big=None)
DecimalType() is decimal(10,0)
The last two lines show three rules for money:
- A
doubleis a binary fraction.0.1D + 0.2Dis0.30000000000000004. This error is acceptable for a distance in miles. It is not acceptable for a sum of fares. - A
decimalis exact, and it has a fixed scale.CAST(123.456 AS DECIMAL(5,2))rounds to123.46. A value with too many digits before the point causes an error.try_castreturns NULL for that value. DecimalType()without arguments isdecimal(10,0). It has no digits after the point. Always give the precision and the scale for money, for exampleDECIMAL(12,2).
Chapter 10 shows the full set of ANSI errors and the try_ functions that return NULL.
Step 8: Two timestamp types, intervals and TIME¶
Spark has two timestamp types. If you use the wrong type, the times can move by some hours.
TIMESTAMP, also calledTIMESTAMP_LTZ, is an instant on the world timeline. Spark keeps it as microseconds since 1970-01-01 00:00 UTC. Spark uses the session time zone (spark.sql.session.timeZone) to convert text to an instant and back.TIMESTAMP_NTZis a wall-clock value with no time zone. It does not change with the session time zone. The taxi columns are this type, because the TLC records local New York time.
type_system.py parses the same text under two session time zones, and converts each value to seconds since 1970 UTC:
q = ("SELECT TIMESTAMP '2026-06-01 10:00:00' AS ts, TIMESTAMP_NTZ '2026-06-01 10:00:00' AS ntz, "
"unix_timestamp(TIMESTAMP '2026-06-01 10:00:00') AS ts_epoch, "
"unix_seconds(TIMESTAMP_NTZ '2026-06-01 10:00:00') AS ntz_epoch")
for tz in ["UTC", "America/New_York"]:
spark.conf.set("spark.sql.session.timeZone", tz)
== 3. TIMESTAMP vs TIMESTAMP_NTZ under two session time zones
UTC ts_epoch=1780308000 ntz_epoch=1780308000
America/New_York ts_epoch=1780322400 ntz_epoch=1780308000
python TZ=UTC collect: datetime.datetime(2026, 6, 1, 10, 0) | toPandas: 2026-06-01 10:00:00
python TZ=Asia/Ho_Chi_Minh collect: datetime.datetime(2026, 6, 1, 17, 0) | toPandas: 2026-06-01 10:00:00
interval day to second -> datetime.timedelta(days=2, seconds=7200)
interval year to month -> ERROR [NOT_IMPLEMENTED] YearMonthIntervalType.fromInternal is not implemented.
TIME literal -> datetime.time(10, 7)
CAST('10:07' AS TIME) -> ERROR [UNSUPPORTED_TIME_TYPE] The data type TIME is not supported. SQLSTATE: 0A000
type_system.py runs against Spark 4.3.0. The same TIMESTAMP text gives two instants under two session time zones. The TIMESTAMP_NTZ text gives one.The TIMESTAMP text gives two instants, 14,400 seconds (four hours) apart. The TIMESTAMP_NTZ text gives the same number in both zones. (unix_seconds accepts TIMESTAMP_NTZ in 4.3, and it reads the wall clock as UTC.)
TIMESTAMP value is four hours later. The TIMESTAMP_NTZ value does not move.The next two lines show a trap in PySpark. collect() converts a TIMESTAMP to a naive datetime in the time zone of the Python process, not the session time zone. The script runs the same query in two child processes, with the session time zone set to UTC in both. The value that collect() gives changes with the TZ of the process. toPandas() uses the session time zone, so it gives the same value in both. Set spark.sql.session.timeZone in each job, and compare instants inside Spark, not after collect().
A literal or a cast that says only TIMESTAMP gets the type from spark.sql.timestampType. The default is TIMESTAMP_LTZ:
spark.sql.timestampType=TIMESTAMP_LTZ: typeof(TIMESTAMP'2026-06-01 10:00') = timestamp
spark.sql.timestampType=TIMESTAMP_NTZ: typeof(TIMESTAMP'2026-06-01 10:00') = timestamp_ntz
Do not change this configuration in one job only. The same SQL text then gives a different type in different jobs. Write TIMESTAMP_NTZ or TIMESTAMP_LTZ in the code when the difference is important.
Intervals are lengths of time. A day-time interval collects as a timedelta. In 4.3.0, a year-month interval still cannot go to Python. Cast it to a string first.
The TIME type in 4.3.0
The TIME type is a time of day with no date. In 4.3.0 it is behind the internal configuration spark.sql.timeType.enabled, which is false by default. The output shows an inconsistency: the TIME'10:07:00' literal works and collects as datetime.time, but CAST(... AS TIME) fails with UNSUPPORTED_TIME_TYPE. Do not use TIME in production code until the configuration is public.
Step 9: Write a schema down¶
You can write a schema as a DDL string or as a StructType. DDL is short. A StructType is better when a program makes or examines the schema:
ddl = ("id BIGINT NOT NULL, name STRING, tags ARRAY<STRING>, "
"address STRUCT<city: STRING, zip: STRING>, amount DOUBLE COMMENT 'order total in CAD'")
s = StructType.fromDDL(ddl)
built = StructType([
StructField("id", LongType(), False),
StructField("name", StringType()),
StructField("tags", ArrayType(StringType())),
StructField("address", StructType([StructField("city", StringType()), StructField("zip", StringType())])),
])
== 4. DDL, StructType, JSON
struct<id:bigint,name:string,tags:array<string>,address:struct<city:string,zip:string>,amount:double>
id BIGINT NOT NULL,name STRING,tags ARRAY<STRING>,address STRUCT<city: STRING, zip: STRING>,amount DOUBLE COMMENT 'order total in CAD'
root
|-- id: long (nullable = false)
|-- name: string (nullable = true)
|-- tags: array (nullable = true)
| |-- element: string (containsNull = true)
|-- address: struct (nullable = true)
| |-- city: string (nullable = true)
| |-- zip: string (nullable = true)
|-- amount: double (nullable = true)
comment lives in metadata: {'comment': 'order total in CAD'}
fromDDL == hand-built (without amount)? True
{"fields":[{"metadata":{},"name":"id","nullable":false,"type":"long"},{"metadata":{},"na
round trip equal: True
differ only in nullability: == False | simpleString equal True
The output shows the four forms of one schema:
simpleString()gives the types only. It does not show nullability or comments.toDDL()gives DDL that you can put in aCREATE TABLEstatement.StructType.fromDDLandtoDDLuse the SQL parser in the JVM, so they need an active session.treeString()gives the text thatprintSchema()prints. It uses the namelong, and the other forms usebigint. The two names are for one type.json()gives a full JSON document.StructType.fromJsonreads it back, and it does not need a session.
A comment is in the metadata dictionary of the field. The metadata goes through json(), through Parquet files and through most transformations.
StructType equality compares names, types, nullability, field order and metadata. Two schemas that differ only in nullability are not equal, but their simpleString() values are equal. Step 14 uses this fact.
The JSON form is the base of a schema contract. A schema contract is a file that holds the agreed shape of a source. Keep the file with your code in version control. Then code can load the file, compare it with the data that arrives, and read files through it.
Step 10: Nested types and paths¶
Data from other systems usually contains nested values. A struct contains named fields, and each row has the same fields. An array holds a list of values of one type. A map holds key-value pairs whose keys are not known before you read the data. You use a path to get a value from each type:
events = spark.read.json(str(EVENTS))
print(events.schema["user"].dataType["geo"])
print(events.schema.fieldNames())
nested.select(F.col("user.geo.country"), F.col("items")[0], F.col("tags")["color"]).show()
== 5. nested types and paths
StructField('geo', StructType([StructField('country', StringType(), True)]), True)
['_corrupt_record', 'amount', 'event', 'event_type', 'items', 'ts', 'user', 'user_id']
+-------+--------+-----------+
|country|items[0]|tags[color]|
+-------+--------+-----------+
| CA| 10| red|
+-------+--------+-----------+
A StructType uses the same path. schema["user"] is a StructField, its dataType is the nested StructType, and you can index that type again. Part VI teaches the complex types and the functions that operate on them.
Step 11: Nullability: what the analyzer tracks and what file readers drop¶
Each field has a nullable flag. The analyzer calculates the flag for each expression, and the optimizer uses it. plans.py filters a non-nullable column for NULL, and then does the same on a nullable column:
ids = spark.range(5)
print(plan(ids.where("id IS NULL"), "simple").strip())
print(plan(fares.where("fare_amount IS NULL"), "simple").strip())
== 5. nullability in the plan
range: struct<id:bigint> False
== Physical Plan ==
LocalTableScan <empty>, [id#14404L]
--- the same filter on a nullable column
== Physical Plan ==
*(1) Filter isnull(fare_amount#10)
+- *(1) ColumnarToRow
+- FileScan parquet [fare_amount#10,tip_amount#13,tolls_amount#14] Batched: true, DataFilters: [isnull(fare_amount#10)], ... PushedFilters: [IsNull(fare_amount)], ...
a: bigint nullable=False
b: double nullable=True
c: int nullable=True
d: int nullable=False
The id column of spark.range cannot be NULL. Thus the optimizer replaces id IS NULL with false, and it replaces the full query with an empty LocalTableScan. No task reads a row. On fare_amount, the filter stays, and Spark pushes it into the Parquet reader.
The four derived columns show how the analyzer calculates nullability:
id + 1is not nullable, because its input is not nullable.id / 0is nullable, because division gives NULL when the divisor is zero, if ANSI mode is off. The type must allow both modes.when(id > 2, 1)withoutotherwiseis nullable, because the rows that do not match get NULL.coalesce(NULL, 0)is not nullable, because its last argument is a constant that is not NULL.
File readers do not keep the promise of a schema. type_system.py reads the events with a schema that says NOT NULL:
== 6. nullability
struct<event:string,user_id:bigint> [True, True]
events with no user_id: 41 of 61
createDataFrame with None: ERROR [FIELD_NOT_NULLABLE_WITH_NAME] field id: This field is not nullable, but got None.
verifySchema=False: [Row(id=1), Row(id=0)]
Both fields come back nullable, and 41 of the 61 events have no user_id. No error occurs. In the 4.3.0 source, DataSource.resolveRelation calls dataSchema.asNullable for file sources, because nothing guarantees that the files agree with the schema. Thus NOT NULL in a read schema is documentation, not a check. To enforce it, count the NULL values, or write to a table format that enforces constraints (Part IX).
createDataFrame does a check, because it converts Python objects. It rejects None for a non-nullable field. With verifySchema=False, it skips the check, and the None becomes 0, a value that was not in the data.
CAUTION
Do not set verifySchema=False for data that you do not control. A None in a non-nullable field becomes 0, and no error occurs.
Step 12: Inference, and why the result changes¶
If you do not give a schema, the JSON reader reads all the records once to infer the schema. Then it reads them again for the query. The inferred schema depends on the records that the reader saw. drift.py reads each day of the events alone:
== 1. inferred schema, one day at a time
01 struct<amount:double,event:string,ts:string,user_id:bigint>
02 struct<amount:string,event:string,items:array<struct<qty:bigint,sku:string>>,ts:string,user:struct<id:bigint,plan:string>>
03 struct<_corrupt_record:string,amount:struct<currency:string,value:double>,event_type:string,items:array<struct<qty:bigint,sku:string>>,ts:string,user:struct<geo:struct<country:string>,id:bigint,plan:string>>
Three days give three schemas. amount is a double, then a string, then a struct. A job that infers its schema changes its output schema each time a new kind of file arrives. The output also shows four rules of JSON inference:
- The reader sorts the fields by name.
- A whole number becomes
bigint. - If the types of a field do not agree, the field becomes
string. - A timestamp stays a
string, unless you setinferTimestamptotrue.
A number with a decimal point becomes double. The option prefersDecimal makes the reader infer a decimal when the value fits:
Use prefersDecimal for money in JSON, because a double cannot hold 0.1 exactly (Step 7).
Day 3 has a truncated line, so the reader adds a _corrupt_record column. Chapter 33 shows the parse modes and the corrupt record column.
samplingRatio tells the reader to infer from a fraction of the records. This is faster, and the result is less stable:
== 2. samplingRatio over the whole folder (local[4])
1.0 8 columns, 61 rows: struct<_corrupt_record:string,amount:string,event:string,event_type:string,it...
0.1 7 columns, 61 rows: struct<_corrupt_record:string,amount:string,event:string,event_type:string,it...
0.05 5 columns, 61 rows: struct<amount:double,event:string,items:array<struct<qty:bigint,sku:string>>,...
0.01 0 columns, 61 rows: struct<>
At 0.1, the user_id column is not in the schema. At 0.05, amount is a double again, so the string amounts of day 2 become NULL. At 0.01, the sample has no records, and the DataFrame has 61 rows and no columns. The sample uses a fixed seed, so the same files give the same result. But Spark takes the sample in each partition, so a different number of files or cores gives a different result.
Step 13: Read through a schema that you chose¶
A schema that you give removes the inference pass, and it gives the same output shape on each run. Values that do not fit the declared type become NULL. The reader ignores the fields that the schema does not name:
c = spark.read.schema("event STRING, amount DOUBLE").json(str(EVENTS))
(c.groupBy(F.col("_metadata.file_name").alias("file"))
.agg(F.count("*").alias("rows"), F.count("amount").alias("amount_ok"))
.orderBy("file").show(truncate=False))
== 3. an enforced schema
+----------------------+----+---------+
|file |rows|amount_ok|
+----------------------+----+---------+
|events_2026-10-01.json|20 |20 |
|events_2026-10-02.json|20 |15 |
|events_2026-10-03.json|21 |0 |
+----------------------+----+---------+
F.count("amount") counts the values that are not NULL. _metadata.file_name is a hidden column that gives the source file of each row. The counts show how much data did not fit: five string amounts on day 2 and all the struct amounts on day 3. The schema protects the job. The counts tell you what the schema removed.
event STRING, amount DOUBLE. Each square is one row. The type of amount changes each day, and the rows that do not fit become NULL.Parquet is different, because each file keeps the physical type of each column. A read schema that does not agree with a file causes an error, not NULL values. drift.py writes day 1 (amount is a double) and day 2 (amount is a string) to Parquet, and reads both with amount DOUBLE:
== 4. Parquet: type mismatch and mergeSchema
double + string files: ERROR [FAILED_READ_FILE.PARQUET_COLUMN_DATA_TYPE_MISMATCH] Encountered
Data type mismatches when reading Parquet column [amount]. Expected Spark type double, actual Parquet type BINARY.
default struct<amount:double,event:string,ts:string,user_id:bigint,day:int>
mergeSchema struct<amount:double,event:string,ts:string,user_id:bigint,items:array<struct<qty:bigint,sku:string>>,user:struct<id:bigint,plan:string>,day:int>
merge double + string: ERROR [CANNOT_MERGE_SCHEMAS] Failed merging schemas:
[CANNOT_MERGE_INCOMPATIBLE_DATA_TYPE] Failed to merge incompatible data types "DOUBLE" and "STRING".
Without mergeSchema, Spark takes the schema from one file (Step 1). Thus the columns that are only in other files are not in the result. mergeSchema reads the footer of each file and makes a union of the columns. spark.sql.parquet.mergeSchema is false by default, because the merge reads all footers. The merge adds columns, but it does not reconcile types. A double in one file and a string in another file stop the read.
Step 14: Detect drift in code¶
Drift is a difference between the schema that you expect and the schema that arrives. There are three kinds of drift: a new field, a field that is not there, and a changed type. A small recursive function finds all three kinds:
def schema_drift(expected, actual, prefix=""):
"""(kind, path, detail) for each field missing, added or changed; nullability ignored."""
drift = []
actual_fields = {f.name: f for f in actual.fields}
for field in expected.fields:
path = prefix + field.name
if field.name not in actual_fields:
drift.append(("missing", path, field.dataType.simpleString()))
continue
e, a = field.dataType, actual_fields[field.name].dataType
if isinstance(e, StructType) and isinstance(a, StructType):
drift.extend(schema_drift(e, a, path + "."))
elif e.simpleString() != a.simpleString():
drift.append(("type_changed", path, f"{e.simpleString()} -> {a.simpleString()}"))
for field in actual.fields:
if field.name not in {f.name for f in expected.fields}:
drift.append(("added", prefix + field.name, field.dataType.simpleString()))
return sorted(drift)
The function compares types with simpleString(), so a change of nullability is not drift. File readers change nullability anyway (Step 11). The function goes into structs, so a new field in user shows as user.geo and not as a change of all of user. drift.py compares the contract with day 1 and with day 3:
== 5. drift against the contract
day 01:
('added', 'user_id', 'bigint')
('missing', 'items', 'array<struct<qty:bigint,sku:string>>')
('missing', 'user', 'struct<id:bigint,plan:string>')
day 03:
('added', '_corrupt_record', 'string')
('added', 'event_type', 'string')
('added', 'user.geo', 'struct<country:string>')
('missing', 'event', 'string')
('type_changed', 'amount', 'double -> struct<currency:string,value:double>')
drift.py runs against Spark 4.3.0. It compares each day of events with the schema contract and reports the fields that changed.Day 3 renamed event to event_type, so the report shows one field as missing and one field as added. A program cannot know that the two are the same field. A person must decide. Decide for each kind of drift what the job does:
| Kind of drift | Example on day 3 | Risk | What the job does |
|---|---|---|---|
added |
event_type, user.geo |
usually safe | Write it to the log. |
missing |
event |
it can be a renamed field | Send it to a person. |
type_changed |
amount: double -> struct |
data loss without an error (Step 13) | Stop the job or send an alert. |
Field order is part of a struct type, and JSON inference sorts fields by name. Thus a contract with STRUCT<sku: STRING, qty: BIGINT> has a different simpleString() from the inferred struct<qty:bigint,sku:string>. Write the nested fields of a contract in alphabetical order. The function compares top-level struct fields by name, but it compares a struct inside an array by its simpleString().
flowchart LR
classDef file fill:#4b5563,fill-opacity:0.72,stroke:#4b5563,color:#ffffff
classDef op fill:#3a5f96,fill-opacity:0.72,stroke:#3a5f96,color:#ffffff
classDef out fill:#c0661a,fill-opacity:0.72,stroke:#c0661a,color:#ffffff
contract["contract.json<br/>(version control)"]:::file --> read["read with<br/>the contract"]:::op
files["new JSON files"]:::file --> read
files --> infer["infer the schema<br/>(profile only)"]:::op
contract --> drift["schema_drift"]:::op
infer --> drift
read --> rows["stable rows,<br/>NULL where values do not fit"]:::out
drift --> report["added, missing,<br/>type_changed"]:::out
Step 15: Combine and reshape DataFrames¶
union combines two DataFrames by column position, not by name. unionByName matches the names. Both fail on the events:
== 6. union, unionByName, allowMissingColumns
union : ERROR [NUM_COLUMNS_MISMATCH] UNION can only be performed on inputs with the same number of columns, but the first input has 4 columns and the second input h
unionByName: ERROR [UNRESOLVED_COLUMN_AMONG_FIELD_NAMES] Cannot resolve column name "event" among (_corrupt_record, amount, event_type, items, ts, user). SQLSTATE: 42703
d1+d2 allowMissing: double
+- *(1) Project [cast(amount#16 as double) AS amount#221, event#17, ts#19, null AS user_id#218L, items#18, use
d1+d3 allowMissing: ERROR [INCOMPATIBLE_COLUMN_TYPE] UNION can only be performed on tables with compatible column types. The first column of the second table is "STRUCT<currenc
allowMissingColumns=True fills a column that one side does not have with NULL. With day 1 and day 2, it works. But day 2 has a string amount, and the result has a double amount. The plan shows the reason.
The union found a common type, and it added cast(amount as double) to the day 2 side. Under ANSI mode, this cast is strict. One amount such as 'n/a' stops the full query with CAST_INVALID_INPUT at run time. With day 1 and day 3, the union fails at analysis, because a struct and a double have no common type.
Thus reshape each source to one target before you combine sources. Spark gives two tools, and each has limits:
try_castreturns NULL for a value that does not convert. It cannot convert between types that have no conversion. A struct cannot become a double:
== 7. try_cast and DataFrame.to
struct try_cast double: ERROR [DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION] Cannot resolve "TRY_CAST(amount AS DOUBLE)" due to data type mismatch: cannot cast "STRUCT<currency: STRIN
DataFrame.to(schema)puts the columns in the order of the schema. It removes the columns that the schema does not name, and it casts where the cast is safe.drift.pyapplies it to a four-column DataFrame (id BIGINT, name STRING, amount DOUBLE, extra STRING) with eight target schemas:
columns in another order Row(name='b', id=2, amount=1.5)
extra column dropped Row(id=2, name='b')
bigint to int Row(id=2, name='b')
double to string Row(id=2, amount='1.5')
string to double ERROR [INVALID_COLUMN_OR_FIELD_DATA_TYPE] Column or field `name` is of type "STRING" while it's r...
nullable to NOT NULL ERROR [NULLABLE_COLUMN_OR_FIELD] Column or field `id` is nullable while it's required to be non-n...
missing nullable column Row(id=2, missing=None)
missing NOT NULL column ERROR [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or function parameter with name `mi...
The "missing nullable column" row does not agree with the 4.3.0 docstring of DataFrame.to. The docstring says that a column that is not in the DataFrame causes a failure. The 4.3.0 behaviour fills them with NULL. Make a test for this behaviour if your code relies on it.
For data that you do not control, a select that you build from the target schema is the most predictable tool. For each target column, it does one of four things:
- If the type is the same, it keeps the column.
- If both types are structs, it builds the target struct field by field.
- If both types are plain types, it uses
try_cast. - In all other cases, it uses a NULL of the target type (
F.lit(None).cast(type)).
def align_column(col, actual, expected):
"""An expression that turns `col` (of type `actual`) into `expected`, NULL where it can't."""
if actual.simpleString() == expected.simpleString():
return col
if isinstance(actual, StructType) and isinstance(expected, StructType):
have = {f.name: f.dataType for f in actual.fields}
parts = [align_column(col.getField(f.name), have[f.name], f.dataType).alias(f.name) if f.name in have
else F.lit(None).cast(f.dataType).alias(f.name) for f in expected.fields]
return F.when(col.isNull(), F.lit(None)).otherwise(F.struct(*parts)).cast(expected)
plain = lambda t: not isinstance(t, (StructType, ArrayType, MapType))
if plain(actual) and plain(expected):
return col.try_cast(expected)
return F.lit(None).cast(expected)
aligned day 1 + day 3: 41 rows, struct<event:string,user:struct<id:bigint,plan:string>,amount:double,i...
+----------+-----------+-----+
|event_null|amount_null|count|
+----------+-----------+-----+
| false| false| 20|
| true| true| 21|
+----------+-----------+-----+
The union now works, and the counts show the cost. All 21 rows of day 3 have no event (the field is now event_type) and no amount (the struct cannot become a double). The aligned DataFrame does not hide this loss. The drift report from Step 14 tells you why it occurred.
Step 16: Column names from other systems¶
Other systems use names that Spark reads in a special way:
== 8. column names from the wild
select('order id') ['order id']
selectExpr('order id') ERROR [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or fu...
selectExpr('`order id`') ['order id']
select('user.name') ERROR [UNRESOLVED_COLUMN.WITH_SUGGESTION] A column, variable, or fu...
select('`user.name`') ['user.name']
select('amount') ['amount']
withColumnsRenamed ['order_id', 'user_name', 'Amount']
JSON amount + Amount ERROR [COLUMN_ALREADY_EXISTS] The column `amount` already exists. C...
CSV header ['id', 'name1', 'name2', '_c3', 'total amount']
Each line shows one rule:
- A space is valid in the DataFrame API. A SQL expression needs backticks around the name.
- A dot means "a field of a struct". A column with a dot in its name needs backticks in the DataFrame API too.
- Case does not matter, because
spark.sql.caseSensitiveisfalseby default.select("amount")findsAmount. But a JSON record with bothamountandAmountfails withCOLUMN_ALREADY_EXISTS. withColumnsRenamedtakes a dictionary. It ignores a name that does not exist ("nope"here) and gives no error.- A CSV header with duplicate names gets numbers (
name1,name2), and an empty name gets its position (_c3).
A join can also give two columns with one name. basics.py joins the blogs table to itself:
== 4. the same column name on two sides
pairs.columns has 2 columns named Hits
select('Hits'): ERROR [AMBIGUOUS_REFERENCE] Reference `Hits` is ambiguous, could be: [`a`.`Hits`, `b`.`Hit
select('a.Hits', 'b.Hits'): 15 rows
The result has two Hits columns. A plain name fails with AMBIGUOUS_REFERENCE, and the message lists the two candidates. Give each side an alias with DataFrame.alias, and then use the qualified names a.Hits and b.Hits. Another method is to rename the columns of one side before the join.
Rename all columns to plain snake_case names immediately after you read a source. Use withColumnsRenamed with a dictionary, or toDF(*names) to replace all the names.
Step 17: From Python data to a DataFrame and back¶
createDataFrame accepts a list of Row objects, a pandas DataFrame or a PyArrow table:
== 7. createDataFrame and toArrow
struct<zone:string,trips:bigint>
struct<zone:string,fare:double>
struct<zone:string,fare:double>
Table id: int64 not null
arrow for pandas conversions: true
A NumPy array also works. Each row of a two-dimensional array becomes a row, and the columns get the names _1, _2 and so on:
From Row objects, Spark infers the types from the Python values, so a Python int becomes bigint. From pandas and PyArrow, Spark takes the types from the source. spark.sql.execution.arrow.pyspark.enabled is true by default in 4.3.0, so pandas conversions in both directions use Arrow. DataFrame.toArrow() (since 4.0) gives a pyarrow.Table, and it keeps the nullability: id is not null. Part VII measures these conversions.
Step 18: DataFrame.zip¶
New in 4.3: DataFrame.zip (SPARK-55886, with Spark Connect support in SPARK-57247) puts the columns of two DataFrames side by side, row by row. The two DataFrames must come from the same plan. Only chains of projections (select, withColumn and similar calls) can make them different. Anything below those chains, such as a filter, a sort, a join or an aggregation, must be the same on both sides. These rules make sure that row n on the left is row n on the right.
base = trips.select("PULocationID", "fare_amount", "tip_amount").limit(3)
left = base.select("PULocationID", "fare_amount")
right = base.select((F.col("tip_amount") / F.col("fare_amount")).alias("tip_pct"))
zipped = left.zip(right)
== 6. DataFrame.zip (4.3)
+------------+-----------+-------------------+
|PULocationID|fare_amount| tip_pct|
+------------+-----------+-------------------+
| 140| 16.3|0.39263803680981596|
| 138| 16.3| 0.4380368098159509|
| 138| 39.4|0.12690355329949238|
+------------+-----------+-------------------+
== Physical Plan ==
CollectLimit 3
+- *(1) Project [PULocationID#7, fare_amount#10, (tip_amount#13 / fare_amount#10) AS tip_pct#14412]
+- *(1) ColumnarToRow
+- FileScan parquet [PULocationID#7,fare_amount#10,tip_amount#13] ...
zip with a filtered side: [ZIP_PLANS_NOT_MERGEABLE] The two DataFrames in zip() cannot be merged. They must produc
zip with a different source: [ZIP_PLANS_NOT_MERGEABLE] The two DataFrames in zip() cannot be merged. They must produc
The physical plan has no join. The analyzer removes the two projection chains, finds the same plan below them, and makes one Project with the columns of both sides. Thus zip costs the same as one select. A filtered side or a different source fails with ZIP_PLANS_NOT_MERGEABLE, because Spark cannot prove that the rows align.
flowchart BT
classDef base fill:#4b5563,fill-opacity:0.72,stroke:#4b5563,color:#ffffff
classDef side fill:#3a5f96,fill-opacity:0.72,stroke:#3a5f96,color:#ffffff
classDef out fill:#c0661a,fill-opacity:0.72,stroke:#c0661a,color:#ffffff
b["base: scan + limit 3"]:::base --> l["left: Project<br/>PULocationID, fare_amount"]:::side
b --> r["right: Project<br/>tip_pct"]:::side
l --> z["zip"]:::side
r --> z
z -->|"analyzer merges<br/>the projections"| one["one Project over base<br/>(no join)"]:::out
Use zip when two parts of a program compute columns from one DataFrame, and you want them in one DataFrame again. Before 4.3, the usual method was zipWithIndex (or monotonically_increasing_id) on both sides and a join on the index. That method costs a shuffle. A join on a key is still the correct tool when the two sides come from different sources.
Step 19: describe, summary and the stat functions¶
A new data set needs a first look: counts, ranges, quartiles and the relations between columns. A DataFrame has methods for each of these, and each method is one Spark job, not a collect(). stats.py runs them on the June 2026 trips. It ran on Spark Classic and on Spark Connect with the same results.
describe and summary¶
describe() gives the count, mean, standard deviation, minimum and maximum of each column. summary() adds the quartiles, or it gives only the statistics that you name:
num = trips.select("passenger_count", "trip_distance", "fare_amount", "tip_amount")
num.describe().show()
num.summary().show()
trips.select("fare_amount", "payment_type").summary("count", "min", "1%", "50%", "99%", "max",
"count_distinct", "approx_count_distinct").show()
== 2. summary(): describe plus the quartiles, or the statistics that you name
+-------+------------------+------------------+-----------------+------------------+
|summary| passenger_count| trip_distance| fare_amount| tip_amount|
+-------+------------------+------------------+-----------------+------------------+
| count| 2823748| 3837248| 3837248| 3837248|
| mean|1.2570296641201693|5.2017636519705865|21.43972443271672|2.9869563239068553|
| stddev|0.6561344679942783| 532.8536966799546|19.17533535065736| 4.070483233040719|
| min| 0| 0.0| -1683.7| -81.97|
| 25%| 1| 1.01| 10.0| 0.0|
| 50%| 1| 1.82| 15.74| 2.24|
| 75%| 1| 3.64| 26.21| 4.11|
| max| 9| 311565.39| 7045.0| 333.33|
+-------+------------------+------------------+-----------------+------------------+
+--------------------+-----------+------------+
| summary|fare_amount|payment_type|
+--------------------+-----------+------------+
| count| 3837248| 3837248|
| min| -1683.7| 0|
| 1%| 3.7| 0|
| 50%| 15.74| 1|
| 99%| 83.5| 2|
| max| 7045.0| 5|
| count_distinct| 12454| 6|
|approx_count_dist...| 11495| 6|
+--------------------+-----------+------------+
The first table finds three problems in seconds:
passenger_counthas 2,823,748 values in 3,837,248 rows. About a quarter of the trips have no passenger count.trip_distancehas a maximum of 311,565.39 miles and a standard deviation of 532.85, but a median of 1.82. A few bad values make the mean and the standard deviation useless.fare_amountandtip_amounthave negative values. The minimum fare is -1683.7.
The quartiles come from percentile_approx, so they are approximate. The values in summary() are strings, because one column holds counts, means and percentiles. count_distinct is exact, and approx_count_distinct uses HyperLogLog++: it gave 11,495 against the exact 12,454 here. The plan shows that summary() is one aggregate over the data:
the plan of summary() is one aggregate over the data:
+- ObjectHashAggregate(keys=[], functions=[count(passenger_count#3L), avg(passenger_count#3L), stddev(cast(passenger_count#3L as double)), min(passenger_count#3L), percentile_approx(passenger_count#3L, [0.25,0.5,0.75], 10000 ...
The stat functions¶
df.stat (or the same methods on the DataFrame, such as df.corr) has the functions for two columns and for samples:
| Method | What it computes | Exact |
|---|---|---|
stat.corr(a, b) |
the Pearson correlation of two numeric columns | yes |
stat.cov(a, b) |
the sample covariance of two numeric columns | yes |
stat.crosstab(a, b) |
a contingency table: one row for each value of a, one column for each value of b |
yes |
stat.freqItems(cols, support) |
the values that occur in more than a fraction support of the rows |
no: the list can have false positives |
stat.approxQuantile(col, probabilities, relativeError) |
quantiles with an error that you set | only with relativeError=0 |
stat.sampleBy(col, fractions, seed) |
a stratified sample with one fraction for each value of col |
no: each fraction is a probability |
== 3. stat.corr and stat.cov (Pearson, two columns, one pass)
corr(trip_distance, fare_amount) = 0.0074
corr(fare_amount, tip_amount) = 0.4339
cov(fare_amount, tip_amount) = 33.87
corr(trip_distance, fare_amount) without outliers = 0.8858
The correlation of distance and fare is 0.0074 on all trips. A taxi fare depends on the distance, so the number is wrong for the question. The bad distances of describe() cause it. With the trips between 0.1 and 100 miles and between 1 and 500 dollars, the correlation is 0.8858. Pearson correlation is sensitive to outliers. Run describe() first, and filter before you correlate.
== 4. stat.crosstab: a contingency table of two columns
+---------------------+------+-------+------+----+-----+---+
|VendorID_payment_type| 0| 1| 2| 3| 4| 5|
+---------------------+------+-------+------+----+-----+---+
| 1|114578| 428201| 57955|7829| 4580| 2|
| 2|890246|1961429|294189|2988|17051| 0|
| 6| 8676| 0| 0| 0| 0| 0|
| 7| 0| 43241| 5660| 388| 235| 0|
+---------------------+------+-------+------+----+-----+---+
The crosstab shows that vendor 6 sends only payment type 0, and vendor 7 never does. The name of the first column of the result is <a>_<b>. Each distinct value of b becomes a column, so use crosstab only on columns with a small number of values.
== 6. stat.approxQuantile: the error you allow sets the cost
relativeError 25% / 50% / 75% of fare_amount time
0.0 [10.0, 15.73, 26.21] 2.33s
0.001 [10.0, 15.85, 26.27] 0.40s
0.01 [10.0, 16.3, 26.2] 0.44s
0.1 [10.0, 14.2, 27.5] 0.49s
several columns at once: [[16.3, 7045.0], [2.24, 333.33]]
relativeError=0 gives the exact quantiles, and it took 2.33 s, more than five times the approximate runs. These times are medians of three runs after a warmup, under the benchmark lock. With 0.001, the median was 15.85 against the exact 15.73. With 0.1, it was 14.2. For a first look, 0.01 is enough.
== 7. stat.sampleBy: a stratified sample with one fraction per key
payment_type 1: 24,490 of 2,432,871 (0.010, asked 0.01)
payment_type 2: 3,614 of 357,804 (0.010, asked 0.01)
payment_type 3: 5,595 of 11,205 (0.499, asked 0.5)
payment_type 4: 10,791 of 21,866 (0.494, asked 0.5)
keys not in fractions get fraction 0: 0 rows of type 0
sampleBy kept 1% of the frequent payment types and 50% of the rare ones. Thus the sample has enough rows of each type to study. Each row goes into the sample with its probability, so the counts are close to the fractions, not equal to them. A value that is not in fractions gets fraction 0.
Step 20: DataFrame.plot¶
Since Spark 4.0, a PySpark DataFrame has a plot accessor, the same API as pandas.DataFrame.plot. It uses plotly, the only backend that it supports. plot.py makes each kind of plot. It ran on Classic and on Connect with the same results:
top = (trips.groupBy("PULocationID").count()
.join(zones, trips.PULocationID == zones.LocationID).orderBy(F.desc("count")).limit(10)
.select("Zone", "count"))
fig = top.plot.bar(x="Zone", y="count") # (1)!
- The result is a plotly
Figure. Show it withfig.show(), or save it withfig.write_htmlorfig.write_image.
top.plot.bar on the ten busiest pickup zones. Spark computed the counts. The plot got ten rows.The important question is how much data goes to the driver. The source (pyspark/sql/plot/core.py) puts each kind of plot into one of three groups, and plot.py shows each group:
session: pyspark.sql.session
spark.sql.pyspark.plotting.max_rows = 1000
== 1. bar: a top-n plot. Aggregate first, then plot the small result
type: plotly.graph_objs._figure.Figure bars: 10
first bar: Upper East Side South 177610
== 2. bar on the raw 3.8 million rows: only the first max_rows rows reach the plot
bars in the figure: 1000
== 3. scatter: a sampled plot
points in the figure: 1000 (a random sample of the filtered rows)
== 4. hist, box and kde: Spark computes the numbers, the driver gets only the result
hist traces: ['fare_amount', 'tip_amount'] bins in each: 20
hist bar heights (fare_amount, first 5): [2006689, 1185285, 361585, 103878, 127386]
box: q1=10.35 median=16.3 q3=26.8 lowerfence=0.0 upperfence=51.47
kde points: 50
== 5. an unsupported backend
PySparkValueError UNSUPPORTED_PLOT_BACKEND | `matplotlib` is not supported, it should be one of the values from plo
| Group | Kinds | What goes to the driver |
|---|---|---|
| top-n | bar, barh, pie |
the first spark.sql.pyspark.plotting.max_rows rows (1,000) |
| sampled | scatter, line, area |
a random sample of about max_rows rows |
| computed in Spark | hist, box, kde |
only the bins, the quartiles and fences, or the density points |
flowchart LR
classDef sp fill:#3a5f96,fill-opacity:0.72,stroke:#3a5f96,color:#ffffff
classDef dr fill:#c0661a,fill-opacity:0.72,stroke:#c0661a,color:#ffffff
df["DataFrame<br/>3.8 million rows"]:::sp --> t["bar, barh, pie:<br/>limit(max_rows)"]:::sp
df --> s["scatter, line, area:<br/>sample to max_rows"]:::sp
df --> c["hist, box, kde:<br/>aggregate in Spark"]:::sp
t --> p["toPandas():<br/>at most 1,000 rows"]:::dr
s --> p
c --> r["bins, quartiles,<br/>density points"]:::dr
p --> f["plotly Figure"]:::dr
r --> f
The bar plot on the raw rows has 1,000 bars: the first 1,000 trips, not a view of the month. A top-n plot does not aggregate for you. Aggregate first, as in the first example. The scatter plot has 1,000 points from a random sample of the filtered rows:
plot.scatter on the trips between 0.1 and 30 miles. The plot shows a sample of 1,000 trips, not all of them. The points at 70 dollars match the most common JFK fare in Chapter 13.The histogram, the box plot and the density plot computed their numbers in Spark over all the rows. The histogram of two columns uses one set of 20 bins for both columns. The box plot uses percentile_approx, so its quartiles are approximate. Its options precision and whis set the accuracy and the length of the whiskers.
The accessor supports only plotly. Other backends fail with UNSUPPORTED_PLOT_BACKEND. The pandas API on Spark has its own plot accessor (Chapter 13).
To run Step 20, install plotly in the environment. kaleido is necessary only for write_image:
The whole chapter, on one page¶
- A DataFrame is a schema and a plan. Methods return new DataFrames with one more plan node. Only
spark.readcan run a job before an action, and for Parquet that job reads one footer. - The everyday methods (
where,withColumns,drop,withColumnRenamed,orderBy,selectExpr) each return a new DataFrame.dropignores a name that is not there. A self join needs aliases. - The analyzer resolves names, types and nullability. It adds casts and attribute IDs. In Classic PySpark it runs for each new DataFrame. On Connect it runs on the server when the plan arrives.
withColumnadds oneProjectper call. The optimizer collapses them, so the tasks are the same. But a loop ofwithColumncalls increases the driver time quickly. UsewithColumnsor oneselect.- Use
DECIMAL(p,s)for money andTIMESTAMP_NTZfor wall-clock times. Setspark.sql.session.timeZonein each job, and compare instants in Spark, not aftercollect(). - Nullability is information for the optimizer, not a check on files. File readers make all fields nullable.
createDataFramechecks nullability unless you disable the check. - Inference changes with the data. Read with a schema contract, infer only to find drift, and count the values that the contract changed to NULL.
- Reshape each source to the contract before a union.
unionByName(allowMissingColumns=True)can add a strict cast.try_castprotects values, not types. DataFrame.zip(4.3) combines projections of one DataFrame without a join.describe,summaryanddf.statgive a first look in one job each. Filter outliers beforecorr. SetrelativeErrorfor quantiles: 0 is exact and costs the most.DataFrame.plot(4.0+, plotly) sends at mostmax_rowsrows for bar and scatter plots. Aggregate first. Spark computes histograms, box plots and density plots.
All of these points come from one fact. A DataFrame is a description. Spark examines that description before it runs anything. Chapter 10 uses the same expressions to clean values that do not fit their types.
Further reading¶
- PySpark DataFrame API, with
zip,to,withColumnsandunionByName. - Data types, the official list of types and their Python and Java values.
- Datetime patterns and time zones, to parse and print timestamps.
- JSON data source options, with
samplingRatio,inferTimestampandprefersDecimal.