// Reproducible California migration figures from the U.S. Census Bureau Vintage 2025 state population estimates. // Source file (public): https://www2.census.gov/programs-surveys/popest/datasets/2020-2025/state/totals/NST-EST2025-ALLDATA.csv // Usage: node census-california-migration.mjs prints the tables below as JSON (also written to census-california-migration-output.json) // Method: DOMESTICMIGyyyy = net domestic migration (people moving between states, estimated), INTERNATIONALMIGyyyy = net international migration. Each column is the July 1 to June 30 estimate year ending in yyyy. // Limits: these are modeled estimates, are revised each vintage, count people not households, and say nothing about why people move or about moving-company volume. import fs from 'node:fs'; const URL_ = 'https://www2.census.gov/programs-surveys/popest/datasets/2020-2025/state/totals/NST-EST2025-ALLDATA.csv'; const txt = await (await fetch(URL_)).text(); const split = (l) => { const c = []; let cur = '', q = false; for (const ch of l) { if (ch === '"') q = !q; else if (ch === ',' && !q) { c.push(cur); cur = ''; } else cur += ch; } c.push(cur); return c; }; const lines = txt.trim().split(/\r?\n/); const H = split(lines[0]); const rows = lines.slice(1).map(split).filter((r) => r[H.indexOf('SUMLEV')] === '040' && r[H.indexOf('NAME')] !== 'Puerto Rico'); // 50 states and DC; Puerto Rico has no state-to-state domestic migration const col = (n) => { const i = H.indexOf(n); if (i < 0) throw new Error('column missing: ' + n); return i; }; const YEARS = [2021, 2022, 2023, 2024, 2025]; const state = (name) => rows.find((r) => r[col('NAME')] === name); const series = (name, pfx) => YEARS.map((y) => Number(state(name)[col(pfx + y)])); const out = { source: URL_, retrieved: new Date().toISOString().slice(0, 10), years: YEARS, states: {}, rankings: {} }; for (const n of ['California', 'Texas', 'Florida', 'New York', 'Arizona', 'Nevada']) { const dom = series(n, 'DOMESTICMIG'), intl = series(n, 'INTERNATIONALMIG'); out.states[n] = { pop2020base: Number(state(n)[col('ESTIMATESBASE2020')]), pop2025: Number(state(n)[col('POPESTIMATE2025')]), domestic: dom, international: intl, domesticCumulative: dom.reduce((a, b) => a + b, 0), internationalCumulative: intl.reduce((a, b) => a + b, 0) }; } const cum = rows.map((r) => ({ name: r[col('NAME')], domestic: YEARS.reduce((a, y) => a + Number(r[col('DOMESTICMIG' + y)]), 0) })).sort((a, b) => a.domestic - b.domestic); out.rankings.largestCumulativeDomesticLosses = cum.slice(0, 5); out.rankings.largestCumulativeDomesticGains = cum.slice(-5).reverse(); out.rankings.californiaRankByYear = Object.fromEntries(YEARS.map((y) => { const s = rows.map((r) => Number(r[col('DOMESTICMIG' + y)])).sort((a, b) => a - b); return [y, s.indexOf(Number(state('California')[col('DOMESTICMIG' + y)])) + 1]; })); out.rankings.note = 'Rank 1 = largest net domestic loss that year among the 50 states and DC (51 rows; Puerto Rico is excluded because the file reports no state-to-state domestic migration for it).'; // All states and DC: one row per state, for the downloadable CSV. const csvRows = rows.map((r) => { const dom = YEARS.map((y) => Number(r[col('DOMESTICMIG' + y)])), intl = YEARS.map((y) => Number(r[col('INTERNATIONALMIG' + y)])); return [r[col('NAME')], ...dom, dom.reduce((a, b) => a + b, 0), ...intl, intl.reduce((a, b) => a + b, 0), Number(r[col('ESTIMATESBASE2020')]), Number(r[col('POPESTIMATE2025')])]; }).sort((a, b) => a[6] - b[6]); const csv = ['state', ...YEARS.map((y) => 'net_domestic_' + y), 'net_domestic_2021_2025', ...YEARS.map((y) => 'net_international_' + y), 'net_international_2021_2025', 'population_estimates_base_april_2020', 'population_estimate_july_2025'].join(',') + '\n' + csvRows.map((r) => `"${r[0]}",${r.slice(1).join(',')}`).join('\n') + '\n'; fs.writeFileSync(new URL('./california-migration-census-2021-2025.csv', import.meta.url), csv); fs.writeFileSync(new URL('./census-california-migration-output.json', import.meta.url), JSON.stringify(out, null, 1)); console.log(JSON.stringify(out, null, 1)); console.log('CSV rows (states + DC):', csvRows.length);