Skip to main content

tlf-hypothesis-testing

Country-agnostic statistical hypothesis testing — part of TLF ("The Living Facts").

Runs seven hypothesis tests over any pandas DataFrame — two-sample, paired, multi-group, and categorical-association tests — and returns each result as a plain dict with the test statistic, p-value, and the sample-size info behind it.

Unlike tlf-census-stats, this package has no dependency on a specific country schema — it works on any DataFrame with the right column shape for the test you choose.


Install

pip install tlf-hypothesis-testing

Or from source, inside the TLF-Data-Analysis monorepo:

cd tlf-hypothesis-testing
pip install -e ".[dev]"

Usage

import pandas as pd
from tlf_hypothesis_testing import HypothesisTestEngine

df = pd.read_csv("census_data.csv")

# Two-sample: compare literacy rate between exactly 2 groups
engine = HypothesisTestEngine(df, test="t_test")  # or "welch_t_test" / "mann_whitney"
engine.run(value_col="literacy_rate", group_col="area_type")

# Paired: compare two matched/before-after numeric columns
engine = HypothesisTestEngine(df, test="paired_t_test")
engine.run(col1="literacy_rate_2011", col2="literacy_rate_2021")

# Multi-group: compare literacy rate across 2+ groups
engine = HypothesisTestEngine(df, test="anova")  # or "kruskal_wallis"
engine.run(value_col="literacy_rate", group_col="province")

# Categorical association between two columns
engine = HypothesisTestEngine(df, test="chi_square")
engine.run(col1="province", col2="area_type")

Tests

Test Column shape Use for
t_test value_col + group_col (exactly 2 groups) Independent samples, assumes equal variance
welch_t_test value_col + group_col (exactly 2 groups) Independent samples, does not assume equal variance
paired_t_test col1 + col2 Matched/before-after samples, same length
mann_whitney value_col + group_col (exactly 2 groups) Non-parametric alternative to the t-test
anova value_col + group_col (2+ groups) Compare means across 3+ groups
kruskal_wallis value_col + group_col (2+ groups) Non-parametric alternative to ANOVA
chi_square col1 + col2 (both categorical) Association between two categorical columns

A group with fewer than 2 non-null observations is dropped automatically before anova/kruskal_wallis run; t_test/welch_t_test/mann_whitney require group_col to split the data into exactly 2 groups, or they raise InvalidGroupCountError.


CLI

tlf-hypothesis-testing --data census.csv --test anova --value-col literacy_rate --group-col province

Run with no flags at all for a fully interactive walkthrough (file path → sheet selection → test → columns → export format). Column prompts are dtype-aware: numeric columns are listed first when picking a value column, and categorical/low-cardinality columns are listed first when picking a group column — each annotated with its type and unique-value count (e.g. Province (categorical, 3 unique)) so it's clear which columns actually make sense for the role, though every column stays selectable either way:

tlf-hypothesis-testing

For unattended/scripted runs, --yes disables all prompting and fails loudly (rather than silently guessing) if something required — like --data — is missing:

tlf-hypothesis-testing --data census.csv --yes --test chi_square --col1 province --col2 area_type

CLI flags

Flag Description
--data Path to a CSV, Excel, or JSON file
--sheet Excel sheet name (default: first sheet)
--test t_test | welch_t_test | paired_t_test | mann_whitney | anova | kruskal_wallis | chi_square
--value-col Value column (two-sample / multi-group tests)
--group-col Group column (two-sample / multi-group tests)
--col1 First column (paired_t_test / chi_square)
--col2 Second column (paired_t_test / chi_square)
--export csv | json
--export-path Export file path. If it doesn't already end in .csv/.json (matching --export), the right extension is appended automatically.
--yes Non-interactive mode: never prompt, error on missing required values

Errors

  • InvalidTestError — unsupported test value
  • InsufficientDataError — a sample/group has too few non-null observations for the test
  • InvalidGroupCountError — group_col doesn't split the data into exactly 2 groups for a two-sample test
  • ColumnNotFoundError — a required column isn't present in the DataFrame

License

MIT — see LICENSE.

Release files for tlf-hypothesis-testing 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tlf-hypothesis-testing 0.1.0
File Size Uploaded
tlf_hypothesis_testing-0.1.0.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tlf-hypothesis-testing 0.1.0
File Interpreter ABI Platform
tlf_hypothesis_testing-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.8 kB

Release files / tlf_hypothesis_testing-0.1.0.tar.gz

Download URL tlf_hypothesis_testing-0.1.0.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f4f76c7a143537a441fe4a8063cfe3e95eddb6a84c3c9da455136ac47fa3e56f
BLAKE2b-256 checksum
How to use checksums
5d982ab63c0b6ef7cf09e87fbc1e42fafcd45549fe895bb254f634e8d5fb6905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / tlf_hypothesis_testing-0.1.0-py3-none-any.whl

Download URL tlf_hypothesis_testing-0.1.0-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b363355186594a4859a7c42ec37c0604ba971c8549ba89d117a6bcaa8bf41fd4
BLAKE2b-256 checksum
How to use checksums
21fe6894b3d4da4f7c320fa877ef0d221ab48e87e59bc2f30c11426fb2039b07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page