Skip to main content

unitsleuth

Detect mixed measurement units in an unlabeled numeric column, from the numbers alone.

A height column where most rows are centimetres and a few are inches, or a temperature column that mixes °C and °F, looks fine to a schema check. unitsleuth fits a mixture model per known unit conversion and tells you which rows were probably recorded in the other unit.

This is a Python implementation of the mixture-model approach of Di Zio, Guarnera and Luzi (2005, Survey Methodology), extended with affine conversions (e.g. °C to °F) and a catalog of common unit pairs. It is not a new method.

Quick start

import numpy as np
import unitsleuth as us

heights = np.concatenate([np.random.normal(170, 9, 900), np.random.normal(67, 3.5, 100)])
best = us.detect(heights, quantity="length")[0]
print(best.label, best.weight, best.detected)
rows = best.converted  # boolean mask of rows likely in the other unit

Whole rows in the wrong unit

When a record was entered in one unit system, every column in that row is affected. detect_rows shares one flag per row across columns, and repair harmonises the data. df can be a pandas or Polars DataFrame (via Narwhals) or a dict of arrays:

cat = us.catalog
fit = us.detect_rows(df, {"height": cat.get("cm", "in"), "weight": cat.get("kg", "lb")})
print(fit.summary())
fixed = us.repair(df, fit)  # same dataframe type, columns corrected

Command line

unitsleuth data.csv --col height:cm,in --col weight:kg,lb --out data_fixed.csv

Prints the report and, with --out, writes a harmonised CSV. The exit code is 1 when mixed units are detected, 0 when not and 2 on usage errors, so it can gate a data pipeline.

Status

Alpha. Univariate and row-consistent multivariate detection and repair work; dataframe integration (pandas, Polars) and a CLI are planned. Detection needs the units to be separated relative to the spread of the data, so it cannot distinguish them when natural variation is wide compared with the conversion factor.

Development

uv sync
uv run pytest

Validation

  • Negative control (real data): on the Palmer penguins dataset (342 birds, 4 numeric columns, 24 catalog conversions each), nothing was flagged: 0 of 96 tests, despite three species with different body sizes.
  • Positive control (semi-real): converting a known 5%, 15% or 30% of the penguins' body mass (kg to lb) and bill length (cm to in) and running detect_rows over 3 random seeds each, the injected rows were found with 100% row accuracy in all 9 runs, and the estimated share was close to the injected share.
  • Limits: these results are for well-separated unit pairs. When the natural spread of a column is wide relative to the conversion factor, the units cannot be told apart from the numbers alone, and the evidence score will say so.

Full guide: docs/usage.md

Metadata

Release files for unitsleuth 0.1.2

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

Source distribution (sdist)

Source distribution for unitsleuth 0.1.2
File Size Uploaded Provenance Actions
unitsleuth-0.1.2.tar.gz 14.2 kB Without provenance Details for unitsleuth-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unitsleuth 0.1.2
File Interpreter ABI Platform Actions
unitsleuth-0.1.2-py3-none-any.whl Python 3 none any Details for unitsleuth-0.1.2-py3-none-any.whl

Total release size: 29.0 kB

Release files / unitsleuth-0.1.2.tar.gz

Download URL unitsleuth-0.1.2.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4d50caf26332ac8166ae3e414de09b74b759239c4df7e2888eb02c50da8dff70
BLAKE2b-256 checksum
How to use checksums
323bc4b7ff732267b1213a8e105fd8debf15cd3caff5188a9356bceed6b24db6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / unitsleuth-0.1.2-py3-none-any.whl

Download URL unitsleuth-0.1.2-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd9a8c2b755a9dea5099ad3de638ab776f753c4b3689b90d1a19176fbe29e8dd
BLAKE2b-256 checksum
How to use checksums
158bba36fb01ec1b64836d29270c2e5b26f5ad6791972231521a3cf8669b1687
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for unitsleuth 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

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