sample-dataset
sample-dataset is a small library for generating balanced, constraint-driven samples from tabular data, using:
- pandas for data handling
- Google OR-Tools CP-SAT for constraint optimization
It allows you to divide your dataset into buckets (e.g., train/test × attribute combinations) while respecting arbitrary minimum size requirements provided by the user.
This is useful for:
- Train/test splits with structural constraints
- Balanced dataset construction
- Controlled sampling for linguistic, NLP, or behavioral datasets
- Any application where "random sampling" must satisfy non-trivial rules
Features
- Constraint-based sampling using OR-Tools
- Flexible bucket definitions via a separate minima dataframe
- Supports arbitrary bucket-defining columns (e.g., split, feature_a, feature_b, split1, split2, ...)
- Automatically infers which dataset rows are eligible for which buckets
- Guarantees minimum bucket sizes
- Supports multiple randomized feasible solutions
- Simple API (assign_buckets, assign_buckets_multiple)
Installation
pip install sample-dataset
Quick Start
Import your data
import pandas as pd
from sample_dataset import assign_buckets
Your dataset (df) might look like:
df = pd.DataFrame({
"ID": [1, 2, 3, 4],
"feature_a": ["a", "a", "su", "su"],
"feature_b": ["yes", "no", "yes", "no"],
"context": ["...", "...", "...", "..."],
})
Define the bucket structure + minima
df_minima = pd.DataFrame({
"split": ["train", "test", "train", "test"],
"feature_a": ["a", "a", "su", "su"],
"feature_b": ["yes", "yes", "no", "no"],
"min_required": [150, 50, 150, 50],
})
Each row represents a bucket. All columns except min_required define the bucket identity.
Assign buckets
df_out = assign_buckets(df, df_minima, verbose=True)
print(df_out.head())
Output:
ID feature_a feature_b context bucket
0 1 a yes ... train|a|yes
1 2 a no ... test|a|no
2 3 su yes ... train|su|yes
3 4 su no ... test|su|no
Multiple randomized balanced samples
To generate N different feasible assignments, use:
from sample_dataset import assign_buckets_multiple
df_samples = assign_buckets_multiple_wide(df, df_minima, n_samples=3)
print(df_samples.head())
You’ll get:
bucket_0 bucket_1 bucket_2
train|a|yes test|a|yes test|a|yes
...
How it works
- The code interprets df_minima as the full set of buckets.
- Matches rows to buckets when all key_cols match
- Enforces minimum bucket sizes
- Forces each row to belong to exactly one bucket
- Uses a randomized objective to obtain diverse feasible assignments
- Solved using OR-Tools’ CP-SAT engine
Requirements
- Python ≥ 3.9
- pandas
- numpy
- ortools
These are installed automatically.
Links
- Source code: [https://github.com/LaboratorioSperimentale/sample-dataset]
- Issues: [https://github.com/LaboratorioSperimentale/sample-dataset/issues]
- PyPI: [https://pypi.org/project/sample-dataset/]
Metadata
Release files for sample-dataset 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sample_dataset-0.3.0.tar.gz | 33.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sample_dataset-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 65.4 kB
Release files / sample_dataset-0.3.0.tar.gz
| Download URL | sample_dataset-0.3.0.tar.gz |
|---|---|
| Size | 33.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37f42994d2855d4116bc1221a3423cb5aa5af738919725d3cf95dfb2d291b0aa
|
|
BLAKE2b-256 checksum How to use checksums |
d45154c2b386e1145ea9c0ebd1791e198351124548ad5ec3314eeb1c793d6589
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / sample_dataset-0.3.0-py3-none-any.whl
| Download URL | sample_dataset-0.3.0-py3-none-any.whl |
|---|---|
| Size | 31.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ddc44a0141a74bdc4c581499dae5a45b1ddc9dd2a321a232e17ab4531d4598d9
|
|
BLAKE2b-256 checksum How to use checksums |
d46b3db71fa31f014681073c9a8ff5d4d0728c80df94960cebf498b600b60a35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|