Skip to main content

A Python library for cleaning and validating pandas DataFrames.

Project description

cleanframe: Lightweight Dataframe Cleaning & Validation

cleanframe is a lightweight Python library designed to streamline the process of cleaning and validating dataframes using a simple, schema-based approach. Define your data's expected structure and properties, and let cleanframe handle the rest, providing a clean dataframe and a detailed report of any issues found.


Features

  • Schema-based Validation: Define your data's structure, rules, and constraints in a simple Python dictionary.
  • Comprehensive Reporting: Get a detailed report of all validation issues, including which columns or rows were affected.
  • Automatic Cleaning: Automatically drop or fill rows that don't conform to your schema.
  • Lightweight & Simple: Designed for ease of use in notebooks, production pipelines, or general data ingestion tasks.
  • Cross-Validation: Perform complex checks on your data, like ensuring one column is a function of others.

Installation

You can install cleanframe directly from PyPI using pip:

pip install cleanframe

Quick Start

Get started in just a few lines of code. The following example demonstrates how to define a schema, validate a sample dataframe, and inspect the results.

from cleanframe.data import sample_data
from cleanframe import clean_and_validate, Schema
import pandas as pd

1. Load your dataframe

df = sample_data()
print("Original DataFrame shape:", df.shape)

2. Define your schema using column and dataframe rules

column_rules = {
    'transaction_id': {
        'dtype': 'string',
        'regex': r'^TXN_\d{7}$',
        'allow_null': False,
        'drop_if_invalid': True
    },
    'customer_id': {
        'dtype': 'string',
        'regex': r'^CUST_\d{2}$',
        'allow_null': False,
        'drop_if_invalid': True
    },
    'category': {
        'dtype': 'category',
        'allowed_values': ['Patisserie', 'Milk Products', 'Butchers', 'Beverages', 'Food', 'Computers and electric accessories'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'price_per_unit': {
        'dtype': 'float',
        'min': 0,
        'max': 10000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 0
    },
    'quantity': {
        'dtype': 'int',
        'min': 1,
        'max': 1000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 1
    },
    'total_spent': {
        'dtype': 'float',
        'min': 0,
        'max': 1000000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 0
    },
    'payment_method': {
        'dtype': 'category',
        'allowed_values': ['Digital Wallet', 'Credit Card', 'Cash'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'location': {
        'dtype': 'category',
        'allowed_values': ['Online', 'In-store'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'transactoin_date': {
        'dtype': 'datetime',
        'allow_null': False,
        'drop_if_invalid': True,
        'max': pd.Timestamp.today()
    },
    'discount_applied': {
        'dtype': 'boolean',
        'allow_null': False,
        'drop_if_invalid': True
    }
}

dataframe_rule = {
    "min_rows": 1,
    "unique_keys": ["transaction_id"],
    "no_duplicates": True,
    "cross_validations": [{"type": "comparison", "condition": "total_spent == price_per_unit * quantity", "action": "drop"}]
}

schema = Schema(rules = column_rules, dataframe_rule = dataframe_rule)

3. Clean and validate your data

cleaned_df, report = clean_and_validate(df, schema)

4. View the report and the cleaned dataframe

print("\nValidation Report:")
print(report)
print("\nCleaned DataFrame shape:", cleaned_df.shape)

Schema Definition

The schema is a Python dictionary that defines the validation rules for your dataframe. It consists of two main parts:

  • column_rules: A dictionary where each key is a column name and the value is a dictionary of rules for that column (e.g., dtype, min, max, regex, allowed_values, allow_null, fillna, and drop_if_invalid).
  • dataframe_rule: A dictionary for rules that apply to the entire dataframe (e.g., min_rows, unique_keys, no_duplicates, cross_validations).

This structure allows for granular control over every aspect of your data's quality.

Contributing

We welcome contributions! If you find a bug or have a suggestion, please open an issue or submit a pull request on our GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cleanframe-0.2.4.tar.gz (223.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cleanframe-0.2.4-py3-none-any.whl (226.5 kB view details)

Uploaded Python 3

File details

Details for the file cleanframe-0.2.4.tar.gz.

File metadata

  • Download URL: cleanframe-0.2.4.tar.gz
  • Upload date:
  • Size: 223.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for cleanframe-0.2.4.tar.gz
Algorithm Hash digest
SHA256 b20ec5815f7f84812e2ad1da29ce3c5f40cf8ba71522691173c9862222edea3c
MD5 63f54b81a93ba855c4475afd808da4f7
BLAKE2b-256 a437ce20afb5367dc77e6c3d96fe712fa002c2c385c3388067e87d2bf4206cb9

See more details on using hashes here.

File details

Details for the file cleanframe-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: cleanframe-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 226.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for cleanframe-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4eaff9a0c2c8455ab0fa9a8509f6e215a1beef3c659494199875a0e0f4684dfb
MD5 62e2f09d0e82b3d9fc4b22216b99e842
BLAKE2b-256 7b879ad297d0c91b157da436b51969292b6c9801e42b6917f00ea0735105ddc6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page