Sub topic 13
Restart and run all: removing hidden state
This chapter assumes you know basic Python, pandas DataFrames, and how to run cells in Jupyter. It covers why a notebook can look correct and still fail on a clean run, and how to check for that.
What hidden state is
A notebook kernel is a running Python process. It remembers every variable you have defined, in the order you ran the cells. It does not remember the order the cells appear on the page.
Hidden state is anything the kernel holds that the saved notebook does not reproduce. The notebook then shows results that a new reader cannot recreate.
Common causes:
- A cell was deleted, but the variable it created still exists in memory.
- Cells were run out of order, so a later cell ran before the cell it depends on.
- A cell was edited and not re-run, so its output is stale.
- A variable was changed in place by one cell, and other cells assume the old value.
A worked example
Suppose your notebook has these cells, in this order.
import pandas as pd
df_raw = pd.read_csv("data/raw/orders.csv", parse_dates=["order_date"])
df = df_raw.copy()df = df[df["amount"] >= 0]
print(len(df))If you run the second cell three times, df is filtered each time. The count stays the same here, but a cell like df["amount"] = df["amount"] * 100 would multiply the amounts again on every run. The output you see depends on how many times you clicked, not on what the file says.
A safer pattern is the one from the cleaning chapter. Wrap each rule in a function that returns a new DataFrame, and assign from df_raw every time.
def clean_orders(df):
out = df.drop_duplicates().copy()
out["amount"] = out["amount"] * 100
return out
df = clean_orders(df_raw)
print(len(df))Running this cell ten times gives the same df, because it always starts from df_raw.
The restart-and-run-all check
The reliable test is to throw away the kernel's memory and run the notebook from the top.
- Save the notebook.
- Use the menu option "Restart Kernel and Run All Cells". In JupyterLab it is under the Kernel menu.
- Wait for every cell to finish.
- Read the outputs from top to bottom.
Compare the new outputs with the old ones. A NameError usually means a cell used a variable that no visible cell defines. A different number or plot means the earlier result depended on stale state.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| `NameError` after restart | Variable was defined in a deleted cell | Add the definition back in a visible cell |
| Row count differs from before | Cell ran several times and changed `df` in place | Rebuild `df` from `df_raw` in one cell |
| Random sample or plot differs | Seed not set, or `rng` used in a different order | Create `rng` once in the setup cell |
| Cell fails only on a fresh run | Import placed in a cell that ran earlier by hand | Move imports to the setup cell |
Making it a habit
- Run restart-and-run-all before you share the notebook and before each commit.
- Do it after any large edit, not only at the end.
- Keep the setup cell first, so the path and seed exist before anything uses them.
- Avoid in-place changes to
df_raw. Create new variables instead.
Exercise
You change a cleaning rule in an early cell, then notice a later histogram still looks the same as before. What are two likely reasons, and what single action checks both?
Show answer
The later cell was not re-run, so its output is stale, or the later cell uses a variable that the changed cell no longer updates, such as an old copy of the DataFrame. Restart the kernel and run all cells in order. The histogram then reflects the current code, and any broken dependency shows up as an error or a changed result.