Two columns in every market table need special handling, and both are about being explicit. A ticker symbol repeats thousands of times, so storing its text on every row is waste. A timestamp looks like one thing but is an integer plus a unit plus possibly a time zone, and mixing those up shifts trades by hours or by a factor of a thousand. Arrow has a type for each, and knowing them prevents some of the most expensive silent bugs in data work.
The idea in plain words
Where we are. Topic 4 showed that a string column pays offsets and characters on every row. Here is the fix, the same idea as Parquet's dictionary encoding: keep each distinct value once. The timestamp half of the topic prepares the capstone, where the ticks carry real times. The last two topics move data between processes.
An array made of two parts: a dictionary holding each distinct value once, and a list of small integer indices, one per row, pointing into it. Reading a row means looking its index up in the dictionary.
Store repeated text once
dictionary_encode swaps a string column for a dictionary one in place. The dictionary holds the five symbols in order of first appearance and every row becomes a 4-byte index. The values read back identically, so code that only looks at values does not notice, while the column takes about 40% fewer bytes here and the saving grows with longer strings and fewer distinct values.
# Dictionary-encode the symbol column and compare its sizecoded = dictionary_encode(table, "symbol")print(coded.schema.field("symbol").type)print("bytes:", f"{table['symbol'].nbytes:,}", "->", f"{coded['symbol'].nbytes:,}")values, indices = dictionary_parts(coded["symbol"].chunk(0))print("dictionary:", values)print("first indices:", indices[:5], "| values read back the same:", coded["symbol"].to_pylist() == table["symbol"].to_pylist())
dictionary<values=string, indices=int32, ordered=0> bytes: 234,451 -> 134,147 dictionary: ['AAPL', 'JPM', 'XOM', 'JNJ', 'KO'] first indices: [0, 0, 0, 0, 0] | values read back the same: True
A timestamp is an integer with a unit
The bars carry a date32: days since 1970. An intraday system needs real times, so as_utc_timestamps converts each date to midnight UTC in the unit you choose. timestamp_ints shows what is really stored: the number of units since 1970-01-01. The same instant is 946,857,600 in seconds and 946,857,600,000 in milliseconds, which is why reading a column with the wrong unit gives dates in 1970 or in the year 50,000.
# Midnight UTC for the first bar, in four units: the same instant, four different integersfor unit in ("s", "ms", "us", "ns"): stamped = as_utc_timestamps(table, "date", unit) print(f"{str(stamped.schema.field('date').type):28} {timestamp_ints(stamped['date'].chunk(0))[0]:>22,}")
timestamp[s, tz=UTC] 946,857,600 timestamp[ms, tz=UTC] 946,857,600,000 timestamp[us, tz=UTC] 946,857,600,000,000 timestamp[ns, tz=UTC] 946,857,600,000,000,000
A timestamp[ms, tz=UTC] and a timestamp[ms] store the same integer; the zone only changes how it is displayed and how local dates are computed. Store trade times in UTC and convert at the edge. Date-only values such as daily bars stay date32, not midnight timestamps, unless a system needs the time.
What the tasks ask
- 1`dictionary_encode` and `dictionary_parts`
table.set_column(index, name, pc.dictionary_encode(column)); for a dictionary array,array.dictionaryandarray.indices. - 2`as_utc_timestamps` and `timestamp_ints`
column.cast(pa.timestamp(unit, tz="UTC"))andset_column;array.cast(pa.int64())reveals the integers.
Lab · what's graded
4 tasks · 100 pts- Store repeated text once:
dictionary_encode25 pts - Take it apart:
dictionary_parts25 pts - Dates to UTC timestamps:
as_utc_timestamps25 pts - The integers behind a time:
timestamp_ints25 pts