Skip to content

Casting functions

Functions

NameDescription
cast_string_to_booleanCasts a column of string values to boolean values.
to_timestampsConverts a column with date/time strings into a timestamp column by trying multiple formats.

cast_string_to_booleanfunction#

def cast_string_to_boolean(column_or_name: ColumnOrName) -> Column

Casts a column of string values to boolean values.

This function converts specific string representations of boolean values to their corresponding boolean types. The recognized string values for False are “False”, “false”, “F”, “f”, and “0”. The recognized string values for True are “True”, “true”, “T”, “t”, and “1”. Any other values will be converted to None.

Parameters

NameTypeDescription
columnColumnThe input column containing string values to be cast.

Returns

ColumnColumn
A column with boolean values where recognized strings are
Column
converted to their corresponding boolean values, and unrecognized
Column
strings are converted to None.

to_timestampsfunction#

def to_timestamps(column_or_name: ColumnOrName, formats: list[str]) -> Column

Converts a column with date/time strings into a timestamp column by trying multiple formats.

This function iterates over a list of date/time formats and attempts to parse the input column using each format. The first format that successfully parses the value is used. If no format succeeds, the result for that row is NULL.

Parameters

NameTypeDescription
column_or_nameColumnOrNameThe input Spark column containing date/time strings to be converted to timestamp format, or the column name.
formatslist[str]A list of date/time format strings to try. Formats should follow the pattern conventions of java.text.SimpleDateFormat, such as “yyyy-MM-dd”, “MM/dd/yyyy”, etc.

Returns

ColumnColumn
A Spark Column of type timestamp. If none of the formats match for a row, the value will be NULL.