Skip to main content

spark dataframe cleaner

Project description

SparkCleaner Documentation

Overview

Disclaimer: this software is not stable yet

SparkCleaner is a Python library for data cleaning using PySpark. It provides various cleaning strategies, a pipeline for applying these strategies, and a logging mechanism for tracking errors and issues.

Cleaning Strategies

  1. DropDuplicatesStrategy

    • Purpose: Removes duplicate rows based on specified columns.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for rows identified as duplicates.
  2. DropMissingValuesStrategy

    • Purpose: Removes rows with missing values in specified columns.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for missing values in specified columns.
  3. FilterNegativeValuesStrategy

    • Purpose: Filters out rows where specified columns have non-positive values.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for negative values in specified columns.
  4. ValidateColumnTypesStrategy

    • Purpose: Ensures columns have the expected data types.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for type mismatches.
  5. ValidateDatesStrategy

    • Purpose: Validates dates in specified columns against a given format.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for invalid dates.
  6. ValidateRegexStrategy

    • Purpose: Ensures column values match specified regex patterns.
    • Method: clean(df: DataFrame) -> DataFrame
    • Logs: Logs errors for values not matching the regex.

Cleaning Pipeline

  • Class: CleaningPipeline
  • Methods:
    • add_strategy(strategy: CleaningStrategy | list[CleaningStrategy]): Adds a cleaning strategy or list of strategies.
    • set_dataframe(df: DataFrame): Sets the DataFrame to be cleaned and adds an __index column.
    • run() -> DataFrame: Applies all strategies to the DataFrame and returns the cleaned DataFrame.
    • get_report() -> str: Returns a JSON report of all logged errors.

Logging

  • Class: Logger
  • Purpose: Records errors encountered during the cleaning process.
  • Method: log_error(index, column, message): Logs errors with details.

Usage Example

from SparkCleaner import *
from pyspark.sql import SparkSession
from pyspark.sql.types import StringType, IntegerType

data = [
    (1, "Alice", 30, "2024-07-01", "alice@example.com"),
    (2, "Bob", None, "2024-07/02", "bob@example"), # null in age col
    (3, "Charlie", 25, "invalid_date", None), # empty email and invalid date
    (4, "David", -5, "2024-07-04", "david@example.com"),
    (5, "Eve", 22, "2024-07-05", "eve@example.com"),
    (5, "Eve", 22, "2024-07-05", "eve@example.com"),
    (5, "Eve", '22', "05-07-2024", "eve@example.com"),  #string value in age column
]
schema = ["id", "name", "age", "date", "email"]

spark = SparkSession.builder \
    .appName("DataCleaningTests") \
    .master("local[*]") \
    .getOrCreate()

df = spark.createDataFrame(data, schema=schema)
cleaning_pipeline = CleaningPipeline()
strategies_pipeline =[
    DropDuplicatesStrategy(columns=df.columns),
    ValidateRegexStrategy(columns=["email"], patterns={'email': '^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$'}),
    DropMissingValuesStrategy(columns=["age"]),
    FilterNegativeValuesStrategy(columns=["age"]),
    ValidateColumnTypesStrategy(columns=df.columns, 
            expected_types={
                'age': IntegerType(), 
                'email': StringType()
                # add more if needed
    }),
    ValidateDatesStrategy(columns=["date"], date_format='yyyy-MM-dd'),
]


cleaning_pipeline.add_strategy(strategy=strategies_pipeline)
cleaning_pipeline.set_dataframe(df = df)

cleaned_df = cleaning_pipeline.run()
errors_report = cleaning_pipeline.get_report()

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

Spark-df-Cleaner-0.0.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

Spark_df_Cleaner-0.0.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file Spark-df-Cleaner-0.0.3.tar.gz.

File metadata

  • Download URL: Spark-df-Cleaner-0.0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for Spark-df-Cleaner-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9811dc10b59078c62b4f376ae7164cb8f4dbdeab06d30eaf016776fc4f981a0d
MD5 768eb23544f66c9e6cef06984c340c9f
BLAKE2b-256 64e1797c961bdace6fddf1d8b6ce8d32285bc191a260392cdaa1bfb874b65769

See more details on using hashes here.

File details

Details for the file Spark_df_Cleaner-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for Spark_df_Cleaner-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 456f54d18a4e2dc0be6cf7cb25713e256b76773603064b783bbb659b7ba2276b
MD5 9b02ef97932ea78c8b0f0f691f501b3c
BLAKE2b-256 6a0fbf68187eb09e89368b759f11c918a4027b15b22f2bf9f61ba538944f224b

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