-
Notifications
You must be signed in to change notification settings - Fork 2
add transformations #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
3a3ee5d
34ff0e0
8267343
c365ecc
178519a
64f7a71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from abc import ABC, abstractmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pandas as pd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import List, Union | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import List, Union, Callable, Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from loguru imort logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TransformBase(ABC): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -50,3 +51,201 @@ def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataframe = t(dataframe) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Identity(TransformBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Returns the original dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This is useful when suming up a lot of transformations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
emptymalei marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| For example, if I have a list of `TransformBase` transformations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| my_transformations = [transform_1, transform_2, transform_3] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transform = sum(my_transformations, Identity()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `transform` will be the chained transformation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warning("This transformation does nothing") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info("Returning the original dataframe") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Shuffle(TransformBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Returns a shuffled dataframe""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info("Returning the original dataframe") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
emptymalei marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ConvertCategoricalType(TransformBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert a column to categorical | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param dt_column: the original datatime column | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
emptymalei marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param target_column: the column to write to. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Default is to overwrite original dt_column | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, column_name: str, target_column: Optional[str] = None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.column_name = column_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if target_column is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target_column = column_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.target_column = target_column | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"converting {self.column_name} to categorical ...") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataframe[self.target_column] = dataframe[self.column_name].astype("category") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.categories = dataframe[self.target_column].cat.categories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataframe[self.target_column] = dataframe[self.target_column].cat.codes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"converted {self.column_name} to categorical!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ReplaceValues(TransformBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Replace some certain values with the specified value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lambda_filter = lambda x: x["indicator_column"] == "bad_value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| replace_val = ReplaceValues( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lambda_filter = lambda_filter, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column_to_replace = "value_a_column", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| replacement_value = np.nan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param lambda_filter: a callable that specifies which row to filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param column_to_replace: which column to replace values with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param replacement_value: the value to replace with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, lambda_filter: Callable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column_to_replace: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| replacement_value: Optional[Any] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.lambda_filter = lambda_filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.column_to_replace = column_to_replace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.replacement_value = replacement_value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"replace values in {self.column_to_replace}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataframe.loc[lambda x: self.lambda_filter(x), self.column_to_replace] = self.replacement_value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class AddColumnWithCondition(TransformBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Add a calculated column based on a lambda function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lambda_filter = lambda x: x["indicator_column"] == "bad_value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| replace_val = ReplaceValues( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lambda_filter = lambda_filter, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column_to_replace = "value_a_column", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| replacement_value = np.nan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param lambda_filter: a callable that specifies which row to filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param lambda_filter: a callable that specifies which row to filter | |
| lambda_compute = lambda x: x["indicator_column"] == "bad_value" | |
| replace_val = ReplaceValues( | |
| lambda_compute = lambda_compute, | |
| column_to_replace = "value_a_column", | |
| replacement_value = np.nan | |
| ) | |
| ``` | |
| :param lambda_compute: a callable that specifies which row to filter |
Copilot
AI
Aug 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter documentation refers to column_to_replace but the actual parameter name is target_column. The documentation should match the parameter name.
| :param column_to_replace: which column to replace values with | |
| :param target_column: which column to replace values with |
Copilot
AI
Aug 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring example shows ReplaceValues but this is the AddColumnWithCondition class. The example should demonstrate AddColumnWithCondition usage instead.
| :param replacement_value: the value to replace with | |
| Example: | |
| ```python | |
| # Suppose you want to add a column "is_bad" that is True if "indicator_column" == "bad_value" | |
| lambda_compute = lambda x: x["indicator_column"] == "bad_value" | |
| add_col = AddColumnWithCondition( | |
| lambda_compute=lambda_compute, | |
| target_column="is_bad" | |
| ) | |
| df = add_col(df) | |
| ``` | |
| :param lambda_compute: a callable that computes the value for each row (applied with DataFrame.apply, axis=1) | |
| :param target_column: the name of the column to add or overwrite |
Copilot
AI
Aug 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter documentation refers to replacement_value but this parameter doesn't exist in the AddColumnWithCondition class. This documentation appears to be copied from another class.
| :param replacement_value: the value to replace with | |
| """Add a calculated column based on a lambda function. | |
| Example: | |
| ```python | |
| # Adds a new column 'is_bad' based on a condition | |
| lambda_compute = lambda x: x["indicator_column"] == "bad_value" | |
| add_col = AddColumnWithCondition( | |
| lambda_compute=lambda_compute, | |
| target_column="is_bad" | |
| ) | |
| ``` | |
| :param lambda_compute: a callable that computes the value for each row | |
| :param target_column: the name of the column to add or overwrite |
Uh oh!
There was an error while loading. Please reload this page.