Skip to main content

If your data is messy - Use Shmessy!

Project description

Shmessy

PyPI version PyPI - Downloads Coverage report License

If your data is messy - Use Shmessy!

Shmessy designed to deal with messy pandas dataframes. We all knows the frustrating times when we as analysts or data-engineers should handle messy dataframe and analyze them by ourselves.

The goal of this tiny tool is to identify the physical / logical data type for each Dataframe column. It based on fast validators that will validate the data (Based on a sample) against regex / pydantic types or any additional validation function that you want to implement.

As you understand, this tool was designed to deal with dirty data, ideally developed for Dataframes generated from CSV / Flat files or any source that doesn't contain strict schema.

Installation

pip install shmessy

Usage

You have two ways to use this tool

Identify the Dataframe schema

import pandas as pd
from shmessy import Shmessy

df = pd.read_csv('/tmp/file.csv')
inferred_schema = Shmessy().infer_schema(df)

Output (inferred_schema dump):

{
    "infer_duration_ms": 91,
    "columns": [
        {
            "field_name": "first_name",
            "source_type": "<class 'numpy.object_'>",
            "inferred_type": "None"
        },
        {
            "field_name": "email",
            "source_type": "<class 'numpy.object_'>",
            "inferred_type": "<class 'str'>",
            "inferred_virtual_type": "<class 'pydantic.networks.EmailStr'>"
        },
        {
            "field_name": "ip_address",
            "source_type": "<class 'numpy.object_'>",
            "inferred_type": "<class 'str'>",
            "inferred_virtual_type": "<class 'ipaddress.IPv4Address'>"
        },
        {
            "field_name": "created_at",
            "source_type": "<class 'numpy.object_'>",
            "inferred_type": "<class 'datetime.date'>",
            "inferred_pattern": "%m/%d/%Y"
        },
        {
            "field_name": "modified_at",
            "source_type": "<class 'numpy.object_'>",
            "inferred_type": "<class 'datetime.datetime'>",
            "inferred_pattern": "%Y-%m-%d %H:%M:%S"
        },
        {
            "field_name": "unixed_at",
            "source_type": "<class 'numpy.float64'>",
            "inferred_type": "<class 'datetime.datetime'>",
            "inferred_pattern": "ms"
        }
    ]
}

Identify and fix Pandas Dataframe

This piece of code will change the column types of the input Dataframe according to Messy infer.

import pandas as pd
from shmessy import Shmessy

df = pd.read_csv('/tmp/file.csv')
fixed_df = Shmessy().fix_schema(df)

Original Dataframe

Original Dataframe

Fixed Dataframe

After fix

API

Constructor

shmessy = Shmessy(
    sample_size: Optional[int] = 1000
)

infer_schema

shmessy.infer_schema(
    df: Dataframe  # Input dataframe
) -> ShmessySchema

fix_schema

shmessy.fix_schema(
    df: Dataframe,
    fix_column_names: Optional[bool] = False  # Replace non-alphabetic/numeric chars with underscore
) -> DataFrame

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

shmessy-1.0.0.tar.gz (6.9 kB view hashes)

Uploaded Source

Built Distribution

shmessy-1.0.0-py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 3

Supported by

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