Skip to content

Spark extensions

Functions

NameDescription
check_table_existsChecks if a specific table exists in the given catalog.
column_functionEvaluates a Column expression in the context of a single-row DataFrame.
convert_1d_list_to_dataframeConverts a 1-dimensional list into a PySpark DataFrame.
convert_dict_to_dataframeTransforms a dictionary with list values into a Spark DataFrame.
createDataFrame_from_dictCreates a Spark DataFrame from a dictionary in a pandas-like style.

check_table_existsfunction#

def check_table_exists(spark: SparkSession, catalog: str, database: str, table_name: str) -> bool

Checks if a specific table exists in the given catalog.

Parameters

NameTypeDescription
sparkSparkSessionThe Spark session.
catalogstrThe catalog name.
table_namestrThe name of the table to check.

Returns

boolbool
True if the table exists, False otherwise.

column_functionfunction#

def column_function(spark: SparkSession, column_obj: Column) -> DataFrame

Evaluates a Column expression in the context of a single-row DataFrame.

This function creates a DataFrame with a single row and applies the given Column expression to it. This is particularly useful for testing Column expressions, evaluating complex transformations, or creating sample data based on Column operations.

Parameters

NameTypeDescription
sparkpyspark.sql.SparkSessionThe SparkSession object.
column_objpyspark.sql.ColumnThe Column object or expression to evaluate.

Returns

DataFrame
pyspark.sql.DataFrame: A single-row DataFrame containing the result of the Column expression.

convert_1d_list_to_dataframefunction#

def convert_1d_list_to_dataframe(spark: SparkSession, list_: list[Any], column_names: str | list[str], axis: str = 'column') -> DataFrame

Converts a 1-dimensional list into a PySpark DataFrame.

This function takes a 1-dimensional list and converts it into a PySpark DataFrame with the specified column names. The list can be converted into a DataFrame with either a single column or a single row, based on the specified axis.

Parameters

NameTypeDefaultDescription
sparkSparkSessionrequiredThe Spark session to use for creating the DataFrame.
list_listrequiredThe 1-dimensional list to convert.
column_namesstr or list of strrequiredThe name(s) of the column(s) for the DataFrame.
axisstr'column'Specifies whether to convert the list into a single column or a single row. Acceptable values are “column” (default) and “row”.

Returns

DataFrameDataFrame
A PySpark DataFrame created from the 1-dimensional list.

Raises

AttributeError
If the axis parameter is not “column” or “row”.

convert_dict_to_dataframefunction#

def convert_dict_to_dataframe(spark: SparkSession, dict_: dict[str, Any], column_names: list[str], explode: bool = False) -> DataFrame

Transforms a dictionary with list values into a Spark DataFrame.

Parameters

NameTypeDescription
dict_dictThe dictionary to transform. Keys will become the first column, and values will become the second column.
column_nameslist[str]A list containing the names of the columns.

Returns

DataFrame
pyspark.sql.DataFrame: A DataFrame with the dictionary keys and their corresponding exploded list values.

createDataFrame_from_dictfunction#

def createDataFrame_from_dict(spark: SparkSession, dict_: dict) -> DataFrame

Creates a Spark DataFrame from a dictionary in a pandas-like style.

Parameters

NameTypeDescription
sparkSparkSessionThe SparkSession object.
dict_dictThe dictionary to convert, where keys are column names and values are lists of column data.

Returns

DataFrameDataFrame
The resulting Spark DataFrame.