pysparky.transformations.general
Functions
| Name | Description |
|---|---|
agg_apply | Apply aggregation expressions and return a DataFrame. |
apply_cols | Apply a function to specified columns of a Spark DataFrame. |
distinct_value_counts_map | Get distinct values from a DataFrame column as a map with their counts. |
execute_transformation_blueprint | Executes a transformation blueprint on a Spark DataFrame. |
filters | Apply multiple filter conditions to a Spark DataFrame. |
get_unique_values | Unions two DataFrames and returns a DataFrame with unique values. |
order_and_aggregate_events | Group events by id, order by record_no, and aggregate into arrays. |
set_columns_to_null_based_on_condition | Sets specified columns to null based on a condition in the given DataFrame. |
transforms | Apply a series of transformations to a Spark DataFrame. |
agg_applyfunction#
def agg_apply(df: DataFrame, agg_exprs: Dict[str, Column]) -> DataFrameApply aggregation expressions and return a DataFrame.
Parameters
Returns
DataFrameDataFrame- A DataFrame containing the aggregated results.
apply_colsfunction#
def apply_cols(sdf: DataFrame, col_func: Callable, cols: list[str] | None = None, **kwargs) -> DataFrameApply a function to specified columns of a Spark DataFrame.
Parameters
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) -> DataFrameGet distinct values from a DataFrame column as a map with their counts.
Parameters
| Name | Type | Description |
|---|---|---|
sdf | DataFrame | The input Spark DataFrame. |
column_name | str | The 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]) -> DataFrameExecutes 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
Returns
DataFrameDataFrame- The resulting DataFrame with the transformed columns.
filtersfunction#
def filters(sdf: DataFrame, conditions: list[Column], operator_: str = 'and') -> DataFrameApply 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
| Name | Type | Default | Description |
|---|---|---|---|
sdf | pyspark.sql.DataFrame | required | The input Spark DataFrame to be filtered. |
conditions | list[pyspark.sql.Column] | required | A 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) -> DataFrameUnions two DataFrames and returns a DataFrame with unique values.
Parameters
| Name | Type | Description |
|---|---|---|
df1 | DataFrame | First DataFrame. |
df2 | DataFrame | Second DataFrame. |
column_name | str | The 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) -> DataFrameGroup events by id, order by record_no, and aggregate into arrays.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
df | DataFrame | required | The input DataFrame. |
id_col | str | required | The column name to group events by. |
value_col | str | required | The column name for values to aggregate. |
record_no_col | str | required | The column name for record numbers to sort by. |
values_array_col | str | required | The column name for the aggregated values array. |
record_nos_array_col | str | required | The column name for the aggregated record numbers array. |
sort_asc | bool | True | Whether 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]) -> DataFrameSets specified columns to null based on a condition in the given DataFrame.
Parameters
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]]) -> DataFrameApply a series of transformations to a Spark DataFrame.
Parameters
| Name | Type | Description |
|---|---|---|
sdf | DataFrame | The input Spark DataFrame to be transformed. |
transformations | list | A 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.