Transform¶
Runs the ETL transformation phase. Applies column renaming, format conversion, and other transformations defined in the datapackage (see Example).
# Run transform using the default datapackage.yaml descriptor
dpetl transform
# Generate a secret key for AES‑SIV anonymization
dpetl transform keygen
Note:
updated_attimestamp is added to the package, and the updated descriptor is saved as a JSON file.
Resource properties¶
Each resource can be exported using dpetl's built-in pipeline, an external command, or both. When both are used, the command runs after the pipeline, so it can further modify the file dpetl has already written.
After transformation, each resource becomes a single file, with updated path, extrapaths (removed), scheme, format and compression (if any) values, and inferred stats.
Tip: Set
dpetl_transform: falseon a package or resource to skip it.
Output settings (for the built‑in pipeline)¶
These are read from the resource's dpetl_transform property:
-
path: optional (defaults todata). Folder where the transformed file is saved. -
format: optional (defaults tocsv.gz, a gzip-compressed .csv).- Supported formats:
csv,txt,xlsx— optionally followed by a compression, likecsv.gz.
- Supported formats:
-
encoding: optional (defaults toutf-8). Used forcsv/txtfiles. -
delimiter: optional (defaults to,). Field separator used forcsv/txtfiles.
cli¶
Runs shell commands as a complement to or replacement for the built‑in pipeline:
-
arguments: list of commands, executed in order. -
path: optional (defaults todpetl_transform.path). Directory where the command's output is expected. -
pre_process: optional (defaults totrue). Iftrue, the built‑in pipeline runs first and the command should (over)write that same file. If false, the built‑in pipeline is skipped and the command alone produces the output file. -
stdin: optional (defaults tofalse). Pipes the built-in pipeline's output to each command's stdin as .csv instead of writing it to disk. -
custom: optional. Defined at the package level, not insidecli. Each key is passed to the commands as an environment variable, with its value written as JSON.
The cli's output must be a file named after the resource (case‑insensitive match). Its format and new columns are inferred from that file.
Field properties¶
Any field in the resource schema may define additional properties to modify its behaviour.
target¶
If a field defines a target property, the field is renamed to the specified target value. After transformation, the target property is removed.
replace¶
Replaces parts of the field's values, as {regex: replacement}. Each regex is applied in order, before join and anonymize.
-
Use
''as replacement to remove the matched text. -
Escape special characters (
'\.') or use brackets ('[.-]') to match them literally. -
Use
^and$to replace only the whole value, e.g.'^INICIADO$': iniciado.
Values that are not strings, such as missing values, are kept.
join¶
Adds columns from a .csv file. Useful for attaching labels or descriptions to codes.
-
path: .csv file to read, relative to the directory wheredpetlruns. -
columns: columns to add, in order, as{name in the resource: column in the file}. -
key: optional (defaults to the field name). Column of the file matched against the field. -
by: optional. Other fields to match, as{field in the resource: column in the file}. They must come before the field in the schema. -
descriptor: optional.pathandresourceof a datapackage to copy the fields of the columns from (defaults tostringfields).
Unmatched rows get empty values. Identical duplicate rows are dropped; for a key with different values, the first row is kept.
Tip: use
dpetl --verboseto see details about the join.
compute¶
Adds columns computed by Python expressions, as {name: expression}. Useful for combining or deriving values from other fields.
Each expression is evaluated per row, with the row's values available by field name (e.g. 'elemento_cod + item_cod.zfill(2)').
The columns are added right after the field, as string fields, after join and before anonymize.
anonymize¶
Fields can be anonymized during transformation by defining an anonymize property.
Supported method values:
-
sha256: deterministic hash (first 16 hex chars) – requires no secret key. -
aes_siv: deterministic encryption (AES-SIV) – requiresANONYMIZE_SECRET_KEY.-
context: optional context tweak – uses another field's value as a cryptographic context, so identical values in different contexts produce different tokens. -
annotation: optional surrogate annotation – a human‑readable prefix prepended to the encrypted value. -
ANONYMIZE_SECRET_KEY: to generate a secret key for AES‑SIV, usedpetl transform keygen.
-
-
[pattern]: mask pattern, e.g.[###-###]or[###-###|####-####].-
#preserves the original digit. -
*masks the digit (replaces with*). -
Other characters are literals.
-
You can also add a filter condition (a Python expression) to control which rows are anonymized.
Example¶
Resources use dpetl_transform and the field properties described above. See the complete example.
resources:
# Column renaming
- name: customers
path: data/customers.csv
schema:
fields:
- name: Name
type: string
target: customer_name
- name: Region
type: string
target: customer_region
dpetl_transform:
format: csv.gz # optional (Defaults to csv.gz)
path: data/processed # optional (Defaults to data)
encoding: utf-8 # optional (Defaults to utf-8)
delimiter: ';' # optional (Defaults to ,)
# Anonymization of sensitive fields
- name: employees
path: data/employees.csv
schema:
fields:
- name: employee_name
type: string
target: name_hashed
anonymize:
method: '[#***]'
- name: tax_id
type: string
target: tax_id_encrypted
anonymize:
method: aes_siv
context: department
annotation: TAX:11
filter: 'department == "Finance"'
- name: phone
type: string
anonymize:
method: '[###-***-####]'
# Replace parts of the values
- name: revenues
path: data/revenues.csv
schema:
fields:
- name: revenue_code
type: string
replace:
'[.-]': '' # {regex: replacement}, applied in order
- name: status
type: string
replace:
'^TEXT$': text
# Join columns from another file
- name: sales
path: data/sales.csv
schema:
fields:
- name: year
type: integer
- name: product_code
type: string
join:
path: data/products.csv # required. Relative to where dpetl runs
columns: {name: product_name, category: category} # required. Columns to add, in order
key: product_code # optional (Defaults to the field name)
by: {year: year} # optional. {field in the resource: column in the file}
# Columns computed from other fields
- name: expenses
path: data/expenses.csv
schema:
fields:
- name: element_code
type: string
- name: item_code
type: string
compute:
element_item_code: 'element_code + item_code.zfill(2)' # {name: Python expression}
# External command
- name: payroll
path: data/payroll.csv
dpetl_transform:
cli:
arguments: # required. List of shell commands to run sequentially
- python scripts/clean_payroll.py
path: data/processed # optional (Defaults to dpetl_transform.path)
pre_process: true # optional (Defaults to true)
stdin: true # optional (Defaults to false)
# Package level. Passed to every cli command as an environment variable
custom:
- year: 2027
region: south
See the API reference.