Skip to main content

Flexible framework for transforming Pandas DataFrames using a modular pipeline approach.

Project description

⚙️ DataMorphers

Unit Tests codecov Code style: black PyPI version

Overview

DataMorphers is a Python library that provides a flexible framework for transforming Pandas DataFrames using a modular data pipeline approach. Transformations are defined in a YAML configuration, and are applied sequentially to your dataset.

By leveraging DataMorphers, your pipelines become cleaner, more scalable and easier to debug.

Features

Installation

Install DataMorphers in your project directly from PyPI:

pip install datamorphers

Usage

1. Define your initial DataFrame

item item_type price discount_pct
apple food 3 0.1
TV electronics 100 0.05
banana food 2.5 nan
pasta food 3 0.12
cake food 15 nan

2. Define Your Transformation Pipeline

Imagine that we want to perform some actions on the original DataFrame. Specifically, we want to identify which items are food, and then calculate the price after a discount percentage is applied. After these operations, we want to polish the DataFrame by removing non interesting columns.

To do so, we create a YAML file specifying a pipeline of transformations, named config.yaml:

pipeline_food:
  # Create a column named "food_marker", containing a fixed value named "food".
  - CreateColumn:
      column_name: food_marker
      value: food

  # Compare the columns "item_type" and "food_marker", and keep rows that are equal (logic: "eq").
  - FilterRows:
      first_column: item_type
      second_column: food_marker
      logic: eq

  # Some values in the column "discount_pct" are NaN. Fill them with 0.
  - FillNA:
      column_name: discount_pct
      value: 0

  # Multiply the columns "price" and "discount_pct". Name the output column "discount_amount".
  - ColumnsOperator:
      first_column: price
      second_column: discount_pct
      logic: mul
      output_column: discount_amount

  # Subtract the columns "price" and "discount_amount". Name the output column "discounted_price".
  - ColumnsOperator:
      first_column: price
      second_column: discount_amount
      logic: sub
      output_column: discounted_price

  # Remove non interesting columns from the DataFrame.
  - RemoveColumns:
      columns_name:
        - discount_amount
        - food_marker

3. Apply the transformations as defined in the config

Running the pipeline is very simple:

from datamorphers.pipeline_loader import get_pipeline_config, run_pipeline

# Load YAML config
config = get_pipeline_config("config.yaml", pipeline_name='pipeline_food'))

# Run pipeline
transformed_df = run_pipeline(df, config)

A log visually shows your data pipeline:

- INFO - Loading pipeline named: pipeline_food
- INFO - *** DataMorpher: CreateColumn ***
- INFO -     column_name: food_marker
- INFO -     value: food
- INFO - *** DataMorpher: FilterRows ***
- INFO -     first_column: item_type
- INFO -     second_column: food_marker
- INFO -     logic: e
- INFO - *** DataMorpher: FillNA ***
- INFO -     column_name: discount_pct
- INFO -     value: 0
- INFO - *** DataMorpher: ColumnsOperator ***
- INFO -     first_column: price
- INFO -     second_column: discount_pct
- INFO -     logic: mul
- INFO -     output_column: discount_amount
- INFO - *** DataMorpher: ColumnsOperator ***
- INFO -     first_column: price
- INFO -     second_column: discount_amount
- INFO -     logic: sub
- INFO -     output_column: discounted_price
- INFO - *** DataMorpher: RemoveColumns ***
- INFO -     columns_name: ['discount_amount', 'food_marker']

The resulting DataFrame follows:

item item_type price discount_pct discounted_price
apple food 3 0.1 2.7
banana food 2.5 0 2.5
pasta food 3 0.12 2.64
cake food 15 0 15

Define runtime values in the YAML configuration

DataMorph can work with variables evaluated at runtime, making it very flexible:

pipeline_runtime:
  - CreateColumn:
      column_name: ${custom_column_name}
      value: ${custom_value}

Simply pass the arguments you need when you instantiate the pipeline:

custom_column_name = "D"
custom_value = 888

kwargs = {
  "custom_column_name": custom_column_name,
  "custom_value": custom_value
}

config = get_pipeline_config(
    yaml_path=YAML_PATH,
    pipeline_name="pipeline_runtime",
    **kwargs,
)

df = run_pipeline(df, config=config)

Extending datamorphers with Custom Implementations

Limiting the pipelines to only the basic DataMorphers defined in this library would make this package of little use. For this reason, datamorphers allows you to define custom transformations by implementing your own DataMorphers. These user-defined implementations extend the base ones and can be used seamlessly within the pipeline.

Creating a Custom DataMorpher

To define a custom transformation, create a custom_datamorphers.py file in your project and implement a new class that follows the DataMorpher structure:

import pandas as pd
import numpy as np
from datamorphers.base import DataMorpher

class CalculateCircularArea(DataMorpher):
    def __init__(self, radius_column: str, output_column: str):
        self.radius_column = radius_column
        self.output_column = output_column

    def _datamorph(self, df: pd.DataFrame) -> pd.DataFrame:
        """
        Calculates the area of a circle.
        """
        df[self.output_column] = np.pi * df[self.radius_column] ** 2
        return df

Importing Custom DataMorphers

To use your custom implementations, create a file named custom_datamorphers.py inside your current directory.

The pipeline will first check for the specified DataMorpher in custom_datamorphers. If it's not found, it will fall back to the default ones in datamorphers. This allows for seamless extension without modifying the base package.

Running the Pipeline with Custom DataMorphers

When defining a pipeline configuration in the YAML file, simply reference your custom DataMorpher as you would with a base one:

custom_pipeline:
  - CalculateCircularArea:
      radius_column: radius
      output_column: area_circle

Then, execute the pipeline as usual:

df_transformed = run_pipeline(df, config)

If a custom module is provided, your custom transformations will be used instead of (or in addition to) the built-in ones.


Pre-commit Hooks

To ensure code quality, install and configure pre-commit hooks:

pre-commit install
pre-commit run --all-files

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT License. See LICENSE for details.

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

datamorphers-1.0.3.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

datamorphers-1.0.3-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file datamorphers-1.0.3.tar.gz.

File metadata

  • Download URL: datamorphers-1.0.3.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for datamorphers-1.0.3.tar.gz
Algorithm Hash digest
SHA256 1922d1958683c542a2edbc23dddd5e1eabb377065801fdef06c9dc0c609ff9e9
MD5 48b0dfaeab3f0c4fb0af9672d8144224
BLAKE2b-256 da85fc6c4ed66bd018181b23240f088ce096c7c618dac8a75814ee3915c40bc2

See more details on using hashes here.

File details

Details for the file datamorphers-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: datamorphers-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for datamorphers-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f5e45448edb6a29330251c4fd4c2a50930d3f66c38c2e58ef1604ceb91a6149e
MD5 935bbf36ce84064851a99df58f897e61
BLAKE2b-256 eb9cd7c449e3ec9f916629e79c9bddc9035e8da42ea09a1e79c25f720ae43ce5

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