Skip to content

Camel to snake

Class for converting DataFrame column names from camel case to snake case.

koheesio.spark.transformations.camel_to_snake.camel_to_snake_re module-attribute #

camel_to_snake_re = compile('([a-z0-9])([A-Z])')

koheesio.spark.transformations.camel_to_snake.CamelToSnakeTransformation #

Converts column names from camel case to snake cases

Parameters:

Name Type Description Default
columns Optional[ListOfColumns]

The column or columns to convert. If no columns are specified, all columns will be converted. A list of columns or a single column can be specified. For example: ["column1", "column2"] or "column1"

None
Example

input_df:

camelCaseColumn snake_case_column
... ...
output_df = CamelToSnakeTransformation(column="camelCaseColumn").transform(
    input_df
)

output_df:

camel_case_column snake_case_column
... ...

In this example, the column camelCaseColumn is converted to camel_case_column.

Note: the data in the columns is not changed, only the column names.

columns class-attribute instance-attribute #

columns: Optional[ListOfColumns] = Field(
    default="",
    alias="column",
    description="The column or columns to convert. If no columns are specified, all columns will be converted. A list of columns or a single column can be specified. For example: `['column1', 'column2']` or `'column1'` ",
)

execute #

execute()
Source code in src/koheesio/spark/transformations/camel_to_snake.py
def execute(self):
    _df = self.df

    # Prepare columns input:
    columns = self.df.columns if self.columns == ["*"] else self.columns

    for column in columns:
        _df = _df.withColumnRenamed(column, convert_camel_to_snake(column))

    self.output.df = _df

koheesio.spark.transformations.camel_to_snake.convert_camel_to_snake #

convert_camel_to_snake(name: str)

Converts a string from camelCase to snake_case.

Parameters:

name : str The string to be converted.

Returns:

str The converted string in snake_case.

Source code in src/koheesio/spark/transformations/camel_to_snake.py
def convert_camel_to_snake(name: str):
    """
    Converts a string from camelCase to snake_case.

    Parameters:
    ----------
    name : str
        The string to be converted.

    Returns:
    --------
    str
        The converted string in snake_case.
    """
    return camel_to_snake_re.sub(r"\1_\2", name).lower()