Transformation Utilities¶
Pure DataFrame column transformations.
LLM-powered transformations (with_langchain_embeddings, map_column_with_llm)
live in :mod:spark_fuse.utils.llm.
rename_columns ¶
rename_columns(df: DataFrame, mapping: Mapping[str, str]) -> DataFrame
Rename columns according to mapping while preserving column order.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any source column is missing or the resulting columns collide. |
Source code in src/spark_fuse/utils/transformations.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
with_constants ¶
with_constants(df: DataFrame, constants: Mapping[str, Any], *, overwrite: bool = False) -> DataFrame
Add literal-valued columns using constants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constants
|
Mapping[str, Any]
|
Mapping of column name to literal value. |
required |
overwrite
|
bool
|
Replace existing columns when |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If attempting to add an existing column without |
Source code in src/spark_fuse/utils/transformations.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
cast_columns ¶
cast_columns(df: DataFrame, type_mapping: TypeMapping) -> DataFrame
Cast columns to new Spark SQL types.
The type_mapping values may be str or DataType instances.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any referenced column is missing. |
Source code in src/spark_fuse/utils/transformations.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
normalize_whitespace ¶
normalize_whitespace(df: DataFrame, columns: Iterable[str], *, trim_ends: bool = True, pattern: str = _DEFAULT_REGEX, replacement: str = ' ') -> DataFrame
Collapse repeated whitespace in string columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
Iterable of column names to normalize. Duplicates are ignored. |
required |
trim_ends
|
bool
|
When |
True
|
pattern
|
str
|
Regex pattern to match; defaults to consecutive whitespace. |
_DEFAULT_REGEX
|
replacement
|
str
|
Replacement string for the regex matches. |
' '
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If any referenced column is missing. |
Source code in src/spark_fuse/utils/transformations.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
split_by_date_formats ¶
split_by_date_formats(df: DataFrame, column: str, formats: Iterable[str], *, handle_errors: str = 'null', default_value: Optional[str] = None, return_unmatched: bool = False, output_column: Optional[str] = None) -> Union[DataFrame, Tuple[DataFrame, DataFrame]]
Split df into per-format partitions with safely parsed date columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
Name of the string column containing date representations. |
required |
formats
|
Iterable[str]
|
Iterable of date format strings, evaluated in order. |
required |
handle_errors
|
str
|
Strategy for unmatched rows ( |
'null'
|
default_value
|
Optional[str]
|
Fallback date string when |
None
|
return_unmatched
|
bool
|
When |
False
|
output_column
|
Optional[str]
|
Optional name for the parsed date column; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Tuple[DataFrame, DataFrame]]
|
The combined DataFrame containing all parsed rows. |
Union[DataFrame, Tuple[DataFrame, DataFrame]]
|
When |
Union[DataFrame, Tuple[DataFrame, DataFrame]]
|
DataFrame as a second element. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
For missing columns, duplicate output column, invalid modes, or
unmatched rows when in |
Source code in src/spark_fuse/utils/transformations.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |