Skip to main content

Build Codecov DOI

microdf

Weighted pandas DataFrames and Series for survey microdata analysis.

Why this exists

Survey microdata comes with weights, and analysing it in pandas means getting two things right that pandas will not do for you.

The estimators are not the obvious ones. A weighted median is not the median of weighted values. Weighted variance requires choosing between treating weights as frequencies or as precision. A top-1% share requires deciding what happens to a record that straddles the cutoff. Each of these is a decision, and hand-rolling it per analysis means making it differently each time. microdf makes each choice once, documents it, and tests it — quantiles follow the inverse CDF so they can be checked against R's survey::svyquantile, and variance treats weights as frequencies so integer weights agree with numpy on the replicated sample.

Weights have to survive the pipeline. Before any estimator runs, weights must stay aligned with their rows through merges, filters, grouping and reindexing. When they do not, nothing raises. The pipeline completes and returns a plausible wrong number. This is the harder of the two problems, and it is why microdf carries weights inside the object rather than beside it.

If you are computing a poverty rate or a Gini on weighted survey data, those are the two ways to get a believable-looking wrong answer.

Key Features

  • MicroDataFrame: A pandas DataFrame with an integrated weight column
  • MicroSeries: A pandas Series with integrated weights
  • Weighted operations: All aggregations (sum, mean, median, etc.) automatically use weights
  • Inequality metrics: Built-in Gini coefficient calculation
  • Poverty analysis: Integrated poverty rate and gap calculations

Installation

Install with:

pip install microdf-python

Or for development:

pip install git+https://github.com/PolicyEngine/microdf.git

Usage

import microdf as mdf
import pandas as pd

# Create sample data with weights
df = pd.DataFrame(
    {"income": [10_000, 20_000, 30_000, 40_000, 50_000], "weights": [1, 2, 3, 2, 1]}
)

# Create a MicroDataFrame
mdf_df = mdf.MicroDataFrame(df, weights="weights")

# All operations are weight-aware
print(mdf_df.income.mean())  # Weighted mean
print(mdf_df.income.gini())  # Gini coefficient

Questions

Contact the maintainer, Max Ghenis (max@policyengine.org).

Citation

You may cite the source of your analysis as "microdf release #.#.#, author's calculations."

Metadata

Release files for microdf-python 1.5.7

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

Source distribution (sdist)

Source distribution for microdf-python 1.5.7
File Size Uploaded
microdf_python-1.5.7.tar.gz 64.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for microdf-python 1.5.7
File Interpreter ABI Platform
microdf_python-1.5.7-py3-none-any.whl Python 3 none any Details

Total release size: 135.5 kB

Release files / microdf_python-1.5.7.tar.gz

Download URL microdf_python-1.5.7.tar.gz
Size 64.1 kB
Tags Source
SHA-256 checksum
How to use checksums
070e914c169a94e12739915bf38e3413d1fc1f0ba0f7414d322c35696f94dd6a
BLAKE2b-256 checksum
How to use checksums
c6acd737918342c7ea41b98b94b392dd697dfe3d561029137f8d158e5931a063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / microdf_python-1.5.7-py3-none-any.whl

Download URL microdf_python-1.5.7-py3-none-any.whl
Size 71.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f1f49ef7970d312549fcee2283f8412dbb6cba84046472e8f321d263ec9b2e0
BLAKE2b-256 checksum
How to use checksums
45b77ab07ee82af8ff5031f0932923808fb5ddc6d306e04ae439db373d317d2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

1.5.11

2 release files

1.5.10

2 release files

1.5.9

2 release files

1.5.8

2 release files

This release

1.5.7 This release

2 release files

1.5.6

2 release files

1.5.5

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.10

2 release files

1.3.9

2 release files

1.3.8

2 release files

1.3.7

2 release files

1.3.6

2 release files

1.3.5

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.3.0

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