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.
  7. FilteringStrategy

    • Purpose: Filters rows based on specified boolean conditions.
    • Method: clean(df: DataFrame) -> DataFrame
  8. FillNaStrategy

    • Purpose: Replaces missing values with specified default values.
    • Method: clean(df: DataFrame) -> DataFrame

Cleaning Pipeline

  • Class: CleaningPipeline
  • Methods:
    • add_strategy(strategy: BaseCleaningStrategy | list[BaseCleaningStrategy]): 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.4.tar.gz (7.3 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.4-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: Spark-df-Cleaner-0.0.4.tar.gz
  • Upload date:
  • Size: 7.3 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.4.tar.gz
Algorithm Hash digest
SHA256 2e74e1af041b51b63632d92e89dbeba1910d564ddb87d5ade2139b776ae42056
MD5 dfb9ccd1b739ca9c59ba8a7f5699e709
BLAKE2b-256 8f43aaf1fd92a22cef1d2fa5af9e86ebf34a8b08db67bf0e5dbd92591656ac21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Spark_df_Cleaner-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 23cab46c0835adff6fcf624cd336738e9a4179a709d661484c2638a6cf3528d6
MD5 3fffdfe081c52516111216f6c0ee136f
BLAKE2b-256 fbe67412c84744a0eecc70fcd0c0e16d2a82d58a9a2e50db96c7641278c2c57e

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