Anonymizer

Anonymizer — applies an AnonymizationConfig to a Spark DataFrame.

class scamander.anonymization.anonymizer.Anonymizer(log: HumanLogger | None = None)

Basisklassen: object

Applies a named set of anonymization rules to a Spark DataFrame.

Rules are executed in the order they appear in the config, so the same column can be transformed more than once when needed.

Parameters:

log – Optional HumanLogger (platform.log). Each rule application is logged at DEBUG level when provided.

Example:

from scamander.anonymization import Anonymizer
from scamander.models.anonymization_config import AnonymizationConfig, AnonymizationRule

config = AnonymizationConfig(
    name="customer_pii",
    rules=[
        AnonymizationRule(column="full_name",  method="fake",    faker_provider="name"),
        AnonymizationRule(column="email",      method="hash"),
        AnonymizationRule(column="phone",      method="mask",    visible_suffix=4),
        AnonymizationRule(column="birth_date", method="redact"),
        AnonymizationRule(column="age",        method="generalize",
                          bins=[0.0, 18.0, 65.0, 120.0],
                          labels=["child", "adult", "senior"]),
    ],
)

anonymized_df = Anonymizer(log=platform.log).apply(df, config)
apply(df: DataFrame, config: AnonymizationConfig) → DataFrame

Apply all rules in config to df in sequence.

Parameters:
  • df – Input DataFrame.

  • config – Anonymization configuration containing the rules to apply.

Returns:

Transformed DataFrame with all anonymization rules applied.

Veroorzaakt:
  • ImportError – When a rule requires an optional package that is not installed.

  • ValueError – When a rule references an invalid Faker provider.