The Common Transforms module provides a set of reusable transformation functions to standardize common data manipulation operations across your Spark pipelines.
Removes duplicate rows from a DataFrame based on specified key columns, keeping only the most recent version according to the SourceTimestamp field.
def remove_duplicates(df, keycolumns):
"""
Remove duplicate rows from a DataFrame based on key columns,
keeping only the most recent version by SourceTimestamp.
Args:
df (DataFrame): The source DataFrame
keycolumns (list): List of column names to use as the unique key
Returns:
DataFrame: DataFrame with duplicates removed
"""from kindling.common_transforms import remove_duplicates
# Keep only the latest customer record per customer_id
customer_df = remove_duplicates(customer_df, ["customer_id"])Drops a column from a DataFrame if it exists, otherwise returns the original DataFrame unchanged.
def drop_if_exists(df, column_name):
"""
Drop a column if it exists, otherwise return the original DataFrame.
Args:
df (DataFrame): The source DataFrame
column_name (str): Name of the column to drop if it exists
Returns:
DataFrame: DataFrame with the column dropped if it existed
"""from kindling.common_transforms import drop_if_exists
# Safely remove a column that might not exist in all source files
clean_df = drop_if_exists(raw_df, "temporary_column")-
Standardize Transformations: Use these common transforms across your codebase to ensure consistent data handling.
-
Extend with Care: When adding new common transforms, ensure they are generalized and reusable across multiple use cases.
-
Performance Considerations: The
remove_duplicatesfunction uses window functions, which can be expensive on large datasets. Consider partitioning your data appropriately when using this function. -
Testing: All common transforms should have comprehensive unit tests to verify correct behavior across edge cases.
To add new transforms to this module, follow these guidelines:
- Create a function with clear, descriptive name
- Add proper docstrings explaining parameters and return values
- Implement error handling for common edge cases
- Write unit tests covering normal usage and edge cases
- Document the new function in this guide