pynhts
Typed, survey-weight-aware Python access to US National Household Travel Survey (NHTS) microdata.
Scope and status (0.8.1, alpha). The 2001, 2009, 2017 and 2022 (NextGen) waves are supported. The API may still change. Results are validated against published tables: for 2009, 35 of 40 figures of the Summary of Travel Trends reproduce completely (the other 5 reproduce the estimate; the report's margin of error uses another multiplier), for 2001 40 of 47 (the 7 others are within one unit or come from an older data version); for 2017, 31 of 37 figures of FHWA's Summary of Travel Trends reproduce completely (3 more reproduce the estimate but not the margin of error, 3 are gaps); for 2022, 15 of 18 control totals of the User Guide, and the Taylor-series errors equal R's
surveypackage. Every gap is reported openly in docs/validation.md (tours: 21 of 33).
Install
pip install pynhts
pynhts fetch --replicates # download, verify (SHA-256) and convert; about 0.4 GB of Parquet, plus 1.8 GB of raw CSV that you may delete afterwards
pynhts fetch --wave 2022 needs no replicate weights (4.5 MB); 2009 and 2001 need a separate
replicate archive (400 and 440 MB) for standard errors. Already have the NHTS CSV files?
pynhts register <folder> [--wave 2022] adopts them after checking their hashes, and
pynhts docs --wave 2001|2009|2017|2022 downloads the official documents (also hash-checked).
What it does
import pynhts
data = pynhts.load(2017)
# lazy, validated tables: columns are joined from the table that owns them
trips = data.trips(["TRPMILES", "TRPTRANS", "HHFAMINC", "R_AGE"])
# weighted statistics with jackknife (JKn) standard errors and 95 % margins of error
s = data.survey("person", ["R_AGE_IMP", "DRIVER"])
s.total(domain=s.rows["DRIVER"] == "01") # 223,277 thousand drivers, MOE 827 thousand
data.survey("trip", ["TRPMILES"]).mean("TRPMILES") # 10.70 miles, MOE 0.40
# replicate a published table and see every gap
from pynhts import recipes
print(recipes.to_markdown(recipes.run("stt2017", data)))
# machine-learning bridge
from pynhts import ml
arr = ml.to_arrays(trips, ["TRPMILES", "R_AGE"], target="TRPTRANS", weight="WTTRDFIN")
# NHTS 2022: same interface, Taylor-series errors (strata + households) instead of replicates
d22 = pynhts.load(2022)
d22.survey("trip", ["TRPMILES"]).mean("TRPMILES") # weights="weekday" | "weekend" too
# 2009 and 2001: 100 and 99 jackknife replicates, same interface (children 0-4 are in the 2001 file)
d09 = pynhts.load(2009, replicates=True)
d09.survey("person", ["R_AGE"]).total() # 283,054 thousand persons
# daily rates by the official route, with the unit in the result
from pynhts import rates
rates.daily_rate(d09, "trips", per="person") # 3.79 person trips per person and day
# what a variable means in each wave, and what to watch for
pynhts.concepts.find("VEHAGE", 2009) # top-coded (35 = 25+), grade H, pitfalls
pynhts.concepts.table() # concepts x 4 waves, grade C / H / N
# the waves use different codes: compare and harmonize explicitly (TRPTRANS 02 is a bike in 2017, a van in 2022)
from pynhts import harmonize
harmonize.compare_waves() # 386 variable names: 19 identical, 47 with changed codes
trips22 = d22.trips(["TRPTRANS"], missing="keep")
trips22["mode"] = harmonize.apply(trips22["TRPTRANS"], "mode", wave=2022)
pynhts.dictionary(2022) # every variable with owner table, type, codes
| Module | Purpose |
|---|---|
pynhts.io |
verified download, offline mode, deterministic Parquet cache |
pynhts.core |
codebook-derived schema, NHTSData loader, missing-code handling |
pynhts.stats |
totals, means, proportions, ratios with jackknife (2001, 2009, 2017) or Taylor-series (2022) errors and margins of error |
pynhts.harmonize |
variable inventory of two waves; 19 crosswalk concepts (mode, three trip-purpose schemes, trip class, income, education, race, Hispanic origin, medical condition, urban area, density, vehicle type, fuel ...) with a status per code; race_ethnicity combines race and Hispanic origin |
pynhts.concepts |
machine-readable dictionary: variable, universe, unit, comparability and pitfalls of each concept in each wave |
pynhts.rates |
daily trips and miles per person or household by the official route, with units |
pynhts.recipes |
replication of published tables with honest status per figure |
pynhts.tours |
tour formation (HBW/HBO/NHB, work-tour patterns), FHWA-style chains; flexible home/work rules |
pynhts.ml |
NumPy / PyTorch / graph export (PyTorch optional) |
pynhts.study |
a research statement (wave, sample rules, variables, weighting) to data + sample-flow table + Table 1 + figures + methods text; compare grades the result against a paper's printed numbers |
pynhts.aggregate |
trips to persons, persons to households: no row multiplication, stated missing-value policy, true zeros, diagnostics report |
pynhts.features |
93 documented person, household, trip and vehicle features (formula, eligibility, denominator, weight, waves) with lineage |
pynhts.eda, pynhts.quality |
missing-value reasons, weighted descriptives, correlation that refuses nominal columns, key and weight integrity report |
pynhts.transform |
leakage-safe transformations (fit on training rows, JSON parameters, split by household) |
pynhts.trends |
per-wave harmonized estimates; guarded pooled workflow that refuses non-comparable concepts |
# 0.7.0: from trips to a documented household table, with diagnostics (see docs/analysis.md)
from pynhts import features, aggregate as A, valid
hh = features.create_household_features(d, groups=["composition", "travel", "modes"])
hh.attrs["pynhts_features"] # lineage: sources, formula, missing rule per feature
Why a library
Correct NHTS analysis has many silent traps (a data server that returns HTTP 200 for missing
files, string codes with leading zeros, -1 skips that are not missing data, 107 copied
parent columns in the child tables, a vehicle PERSONID that is not the person key, ...).
They are documented in docs/data_notes.md and each is covered by a test.
Validation
-
All coded values of the official codebooks (counts and weighted totals) reproduce: 3,807 in 2017, 2,639 in 2022, 3,435 in 2009 and 6,327 in 2001 (three documented codebook errors in 2009).
-
37 figures from Tables 1 and 3 of FHWA's Summary of Travel Trends: 2017 NHTS, including the adjusted-distance rows and the annual totals, with margins of error: 31 reproduce completely, 3 reproduce the estimate but not the margin of error, 3 do not reproduce.
-
2022: 15 of the 18 control totals of the V2.x User Guide (Tables 10 and 11); the 3 that do not reproduce are documented. Estimates, standard errors and degrees of freedom of 24 estimands equal R's
surveypackage to 1e-9. -
2009: 40 figures of the Summary of Travel Trends (35 reproduce completely; all 27 printed margins of error of its Table 1 match t(0.975, 100) x the jackknife standard error to rounding). 2001: 47 figures (40 reproduce; the 2004 report's per-person rates are reproduced only with its own definition, documented). The 2001 User's Guide worked example (202,586,200 vehicles, SE 672,072) reproduces.
-
The transit-user sample of McNally & Rafiq's tour study (4,994 persons, 20,222 trips, 10,011 transit trips) exactly, plus 33 of their figures in all: 21 reproduce, 12 gaps are documented (
pynhts.tours). See docs/validation.md. -
Independent evidence (2026-10-22,
docs/paper_data_audit): the analysis data of ten published NHTS papers that pynhts was never tuned on were rebuilt from the public files. The national totals and rates of Lidbe et al. (2021) for 2001 and 2017, Tables 1 to 4 of Alberini et al. (2021) and the 130,222-person sample with 21 odds ratios of Yang et al. (2011) reproduce; where papers do not reproduce, the cause is an unstated sample rule or an inconsistency in the paper (documented per paper, none tuned away). In every household whose person file is a complete roster, the worker and driver counts ofpynhts.featuresequal the survey's ownWRKCOUNTandDRVRCNTin all four waves.
Documentation
Analysis toolkit · Feature registry · Research landscape · Comparing waves · Defaults and guards · AGENTS.md · Methods · Data notes · Validation · Roadmap · Contributing · Changelog
Data
The NHTS microdata are public data of the US Federal Highway Administration (https://nhts.ornl.gov). They are never bundled with or committed to this repository.
Development
uv venv && uv pip install -e ".[dev]"
pytest # unit tests, no data needed
pytest -m data # real-data tests (files registered in the pynhts cache)
pytest -m network -o addopts=-ra # live ORNL download, manual only
Citation
Cite the software with the Zenodo DOI 10.5281/zenodo.23573454 (all versions; version 0.8.0 is 10.5281/zenodo.23573455), see CITATION.cff. License: BSD-3-Clause.
Metadata
Release files for pynhts 0.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pynhts-0.8.1.tar.gz | 2.2 MB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pynhts-0.8.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 2.7 MB
Release files / pynhts-0.8.1.tar.gz
| Download URL | pynhts-0.8.1.tar.gz |
|---|---|
| Size | 2.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a983cc3923ca368f49a85aad54871ed02e1dd0d80697f279e8ac00147c1949d5
|
|
BLAKE2b-256 checksum How to use checksums |
ad6c310a69ec30733e19788b9e6ec83f458d22363228015fbdf191ee99327419
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logRelease files / pynhts-0.8.1-py3-none-any.whl
| Download URL | pynhts-0.8.1-py3-none-any.whl |
|---|---|
| Size | 525.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db7c5383a3160770026dc786a5ae54b01ae705f28366d567f95cd055723f7b88
|
|
BLAKE2b-256 checksum How to use checksums |
73ffbbe372d828ed198c631fc4f4270425aaf8efa77bead1c651a8168ddea0ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logSecurity for pynhts 0.8.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pynhts-0.8.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.