Skip to content

General transformations

Functions

NameDescription
agg_applyApply aggregation expressions and return a DataFrame.
apply_colsApply a function to specified columns of a Spark DataFrame.
distinct_value_counts_mapGet distinct values from a DataFrame column as a map with their counts.
execute_transformation_blueprintExecutes a transformation blueprint on a Spark DataFrame.
filtersApply multiple filter conditions to a Spark DataFrame.
get_unique_valuesUnions two DataFrames and returns a DataFrame with unique values.
order_and_aggregate_eventsGroup events by id, order by record_no, and aggregate into arrays.
set_columns_to_null_based_on_conditionSets specified columns to null based on a condition in the given DataFrame.
transformsApply a series of transformations to a Spark DataFrame.

agg_applyfunction#

def agg_apply(df: DataFrame, agg_exprs: Dict[str, Column]) -> DataFrame

Apply aggregation expressions and return a DataFrame.

Parameters

NameTypeDescription
dfDataFrameThe input Spark DataFrame.
agg_exprsDict[str, Column]A dictionary where keys are the aliases for the aggregated columns and values are the Spark aggregation expressions (Columns).

Returns

DataFrameDataFrame
A DataFrame containing the aggregated results.

apply_colsfunction#

def apply_cols(sdf: DataFrame, col_func: Callable, cols: list[str] | None = None, **kwargs) -> DataFrame

Apply a function to specified columns of a Spark DataFrame.

Parameters

NameTypeDefaultDescription
sdfDataFramerequiredThe input Spark DataFrame.
col_funccallablerequiredThe function to apply to each column.
colslist[str]NoneList of column names to apply the function to. If None, applies to all columns. Defaults to None.

Returns

DataFrameDataFrame
A new Spark DataFrame with the function applied to the specified columns.

distinct_value_counts_mapfunction#

def distinct_value_counts_map(sdf: DataFrame, column_name: str) -> DataFrame

Get distinct values from a DataFrame column as a map with their counts.

Parameters

NameTypeDescription
sdfDataFrameThe input Spark DataFrame.
column_namestrThe name of the column to process.

Returns

DataFrameDataFrame
A DataFrame containing a single column with a map of distinct values and their counts.

execute_transformation_blueprintfunction#

def execute_transformation_blueprint(sdf: DataFrame, blueprint: dict[str, Column]) -> DataFrame

Executes a transformation blueprint on a Spark DataFrame.

The transformation blueprint is a dictionary where keys are column names and values are the corresponding transformations to apply. The function applies each transformation in the order specified by the blueprint and returns the resulting DataFrame with the transformed columns.

A transformation_blueprint is a dictionary that the key: new column name value: Column function

Parameters

NameTypeDescription
sdfDataFrameThe input DataFrame to be transformed.
blueprintDict[str, Column]A dictionary of column names as keys and transformation functions as values.

Returns

DataFrameDataFrame
The resulting DataFrame with the transformed columns.

filtersfunction#

def filters(sdf: DataFrame, conditions: list[Column], operator_: str = 'and') -> DataFrame

Apply multiple filter conditions to a Spark DataFrame.

This function takes a Spark DataFrame, a list of conditions, and an optional operator. It returns a new DataFrame with all conditions applied using the specified operator.

Parameters

NameTypeDefaultDescription
sdfpyspark.sql.DataFramerequiredThe input Spark DataFrame to be filtered.
conditionslist[pyspark.sql.Column]requiredA list of Column expressions representing the filter conditions.
operator_Callable'and'The operator to use for combining conditions. Defaults to and_. Valid options are and_ and or_.

Returns

DataFrame
pyspark.sql.DataFrame: A new DataFrame with all filter conditions applied.

Raises

ValueError
If an unsupported operator is provided.

get_unique_valuesfunction#

def get_unique_values(df1: DataFrame, df2: DataFrame, column_name: str) -> DataFrame

Unions two DataFrames and returns a DataFrame with unique values.

Parameters

NameTypeDescription
df1DataFrameFirst DataFrame.
df2DataFrameSecond DataFrame.
column_namestrThe column name containing the values.

Returns

DataFrameDataFrame
A DataFrame with unique values.

order_and_aggregate_eventsfunction#

def order_and_aggregate_events(df: DataFrame, id_col: str, value_col: str, record_no_col: str, values_array_col: str, record_nos_array_col: str, sort_asc: bool = True) -> DataFrame

Group events by id, order by record_no, and aggregate into arrays.

Parameters

NameTypeDefaultDescription
dfDataFramerequiredThe input DataFrame.
id_colstrrequiredThe column name to group events by.
value_colstrrequiredThe column name for values to aggregate.
record_no_colstrrequiredThe column name for record numbers to sort by.
values_array_colstrrequiredThe column name for the aggregated values array.
record_nos_array_colstrrequiredThe column name for the aggregated record numbers array.
sort_ascboolTrueWhether to sort the arrays in ascending order. Defaults to True.

Returns

DataFrameDataFrame
A DataFrame with events aggregated into arrays and ordered by record_no.

set_columns_to_null_based_on_conditionfunction#

def set_columns_to_null_based_on_condition(df: DataFrame, condition_column: str, condition_value: str, target_columns: tuple[str]) -> DataFrame

Sets specified columns to null based on a condition in the given DataFrame.

Parameters

NameTypeDescription
dfDataFrameThe input DataFrame.
condition_columnstrThe name of the column containing the condition value.
condition_valuestrThe value indicating the condition to set columns to null.
target_columnsTuple[str]The tuple of columns to be set as null if the condition value is found.

Returns

DataFrameDataFrame
The updated DataFrame with specified columns set to null based on the condition.

transformsfunction#

def transforms(sdf: DataFrame, transformations: list[tuple[Callable, dict]]) -> DataFrame

Apply a series of transformations to a Spark DataFrame.

Parameters

NameTypeDescription
sdfDataFrameThe input Spark DataFrame to be transformed.
transformationslistA list of transformations, where each transformation is a tuple containing a function and a dictionary of keyword arguments to apply the function to.

Returns

DataFrameDataFrame
The transformed Spark DataFrame.