Notes

Part 10 of 10

DataFrames, matrices, and arrays

Earlier models serve both OLTP and analytics. DataFrames and multidimensional arrays (matrices) live mainly in analytics and science, rarely OLTP.

DataFrames come in R, Pandas, Spark, ArcticDB, Dask, and others. They are popular for preparing ML training data, exploring data, statistics, and charts. A DataFrame looks like a table or spreadsheet and supports bulk, relational-style steps:

  • Apply a function to every row.
  • Filter rows.
  • Group and total.
  • Join with another DataFrame on a key, called a merge.

Instead of one declarative query, you run a series of commands that reshape the data. That matches how data scientists "wrangle" data step by step, usually on a private copy on their own machine, sharing results later.

DataFrame APIs reach far past relational operations and are used very differently from relational design. A common job is turning table-like data into a matrix or multidimensional array, the input many ML algorithms need.

Example: a table of shoppers rating products 1 to 5 becomes a grid with one row per shopper and one column per product, like a spreadsheet pivot table.

  • It is sparse: most pairs have no rating, and that is fine.
  • With thousands of product columns, a relational database struggles, but DataFrames and sparse-array libraries like NumPy cope easily.

Matrices hold only numbers, so other data gets converted:

  • Dates become decimals scaled into a set range.
  • A column with a few fixed values gets one-hot encoding: one column per value (shoes, shirts, hats), 1 in the matching one and 0 elsewhere, which also works for items in several categories.

A numeric matrix opens the door to linear algebra, the base of many ML methods, like a product recommender. DataFrames let data move gradually from table shape to matrix, under the data scientist's control.

Array databases like TileDB specialize in huge multidimensional numeric arrays: satellite image grids, medical scans, telescope data. DataFrames also serve finance for time series like prices and trades, and batch frameworks like Spark and Flink added them (chapter 11).

Looking back

  • The relational model is over 50 years old and still central, especially for warehouses and business analytics with star and snowflake schemas and SQL.
  • Documents are self-contained JSON, with links between documents being rare.
  • Graphs are the reverse: anything links to anything, and Cypher, SPARQL, and Datalog answer multi-hop questions with recursion.
  • DataFrames stretch table-like data to very many columns and bridge to arrays for ML, statistics, and science.
  • One model can imitate another, like a graph in tables, but awkwardly, like long recursive SQL. So specialist databases exist for each model, while general ones grow into nearby models: JSON in relational, joins in document stores, graph queries in SQL.
  • Event sourcing keeps an append-only log of immutable events, which suits complex business domains; appending is good for write speed (chapter 4), and CQRS turns the log into read views.
  • Non-relational models rarely enforce a schema, which eases change, but the app still assumes a structure, so the real question is explicit (on write) versus implicit (on read).

Key takeaways

  • Declarative queries say what you want; the optimizer decides how.
  • Documents fit self-contained one-to-few trees; tables fit shared, many-to-one, and many-to-many data.
  • Normalize for cheap, safe writes; denormalize for fast reads, and keep the copies in step.
  • Warehouses put a fact table of events at the center, with dimension tables around it.
  • "Schemaless" still has a schema; it is checked on read instead of on write.
  • Graphs suit densely linked data, and Cypher, SPARQL, and Datalog handle multi-hop queries that SQL makes clumsy.
  • Event sourcing stores immutable events, and CQRS derives read views that can always be rebuilt.
  • DataFrames bridge tables and numeric matrices for ML and science.

Go deeper: DDIA, 2nd edition, chapter 3: Data Models and Query Languages

Based on the ideas in Designing Data-Intensive Applications, 2nd edition, by Martin Kleppmann and Chris Riccomini (O'Reilly). Written in our own words; Recurse is not part of the book or its publisher. Get the book