A Delta table remembers its columns and their types: its schema. The first write stores the DataFrame's schema in the log, and every later write is checked against it. If a column's values can't be turned into the table's type, the write is refused with an error and the table doesn't change: no new version, no new rows.
One bad value is enough to change a column's type. 05-03's export has the quantity two on order 5030, so pd.read_csv reads that file's whole quantity column as text (str). A plain Parquet folder would take the file, and every reader of the folder would then find quantity as a number in some files and as text in one. A Delta table stops the bad write at the door, so the job finds the problem the day it happens, not when a report breaks a week later. You catch the refusal with try / except Exception as error:, so the program can say why and go on.
A schema can also grow on purpose. A DataFrame with a new column is refused too (a SchemaMismatchError), unless the write has schema_mode="merge": then the table adds the column, and the rows written before read NULL there.
try:
write_deltalake("visits_delta", bad_visits, mode="append")
except Exception as error:
print("Not written:", error)
write_deltalake("visits_delta", visits_with_device, mode="append", schema_mode="merge")
For this lesson a hidden setup writes a small new export, orders/2026-05-08.csv, with a coupon column:
order_id,customer_id,product,quantity,unit_price,status,ordered_at,coupon 5076,42,Mug,2,12,paid,2026-05-08 09:12:00,SPRING10 5077,133,Pen,5,2,paid,2026-05-08 10:40:00, 5078,207,Teapot,1,22,paid,2026-05-08 13:05:00,SPRING10 5079,88,Lamp,1,29,cancelled,2026-05-08 16:31:00, 5080,265,Plant,3,15,paid,2026-05-08 19:58:00,WELCOME5
Your program prints the refusal and the version after it; a hidden check then prints the table's version, its columns and the quantity and coupon of every order after 5007. The output must be:
quantity in 05-03's file: str Refused: Cast error: Cannot cast string 'two' to value of Int64 type Version: 0 Version now: 1 Columns: ['order_id', 'customer_id', 'product', 'quantity', 'unit_price', 'status', 'ordered_at', 'coupon'] order_id | quantity | coupon 5008 | 1 | NULL 5009 | 1 | NULL 5010 | 3 | NULL 5076 | 2 | SPRING10 5077 | 5 | NULL 5078 | 1 | SPRING10 5079 | 1 | NULL 5080 | 3 | WELCOME5 (8 rows)
Your task: the starting code reads 05-01, 05-03 and 05-08 into day1, day3 and day8, writes day1 as the table orders_delta and prints the type of 05-03's quantity column. In a try block, append day3; catch the error with except Exception as error: and print "Refused:" and error. Then print "Version:" and the version of a freshly opened DeltaTable("orders_delta"), and append day8 with mode="append" and schema_mode="merge".
Assignment
Follow the Coding Tutorial to catch the refused append and then let the schema grow on purpose.
Hint
The append goes inside the try (indented) and the print inside the except. The version print and the last append are not indented. schema_mode="merge" goes after mode="append".
Free plan: 30 tutor messages. PRO is unlimited.
