Python code reference
D.Hub Python code transforms and analyzes data. The primary data-processing library is Polars.
Where it is used
- Create and manage Python code under Code in a collection.
- Add a Python code node to a pipeline, then select an existing code asset or write new code.
- A code node passes its result to the next node in the pipeline.
Execution contract
A Python code node uses the run function as its entry point. Its input is a Polars DataFrame, and it returns a dictionary containing an "output" key.
import polars as pl
def run(input, *, options=None, contexts=None):
# input : Polars DataFrame (input data)
# options : dict (custom options)
# contexts: dict (values shared between steps)
output = input.with_columns(
(pl.col("value") * 2).alias("doubled_value")
)
return {"output": output}
Input input
- A single input is passed as the first parameter,
input(a Polars DataFrame). - A node with multiple inputs receives each input connection's alias as a parameter name. See Accept multiple inputs for an example.
Options options
- Values entered in the code node's Step options are passed in the
optionsdictionary. - Use options for dates, filter conditions, and other values that change between runs.
Shared values contexts
- Values shared between steps are passed in the
contextsdictionary. contextsis always passed at runtime. Declare it as a keyword-only parameter even if the code does not use it.
Output output
- Return a dictionary containing an
"output"key whose value is a Polars DataFrame.
Returning the DataFrame directly, as in return output, causes an error. Return it in the form {"output": output}.
Available packages
The pipeline Python environment provides the following commonly used data-processing and geospatial-analysis libraries.
| Package | Purpose |
|---|---|
polars | DataFrame processing (primary library) |
pandas | General-purpose DataFrame processing |
pyarrow | Apache Arrow columnar-data processing |
numpy | Numerical computing |
geopandas | Geospatial data processing |
h3 | H3 hexagonal spatial indexing |
pyproj | Coordinate-system transformations |
rdflib | RDF and ontology data processing |
Enter packages that are not included in the default environment in the code's Packages field. Ask your administrator which packages are permitted.
Code examples
Filter data
import polars as pl
def run(input, *, options=None, contexts=None):
return {"output": input.filter(
(pl.col("status") == "active") & (pl.col("age") >= 18)
)}
Accept multiple inputs
A node with multiple input connections receives each input's alias as a parameter name. Declare options and contexts as keyword-only parameters after the input parameters.
import polars as pl
def run(orders, customers, *, options=None, contexts=None):
output = orders.join(customers, on="customer_id", how="left")
return {"output": output}
Use options
import polars as pl
def run(input, *, options=None, contexts=None):
threshold = float(options.get("threshold", 100))
return {"output": input.filter(pl.col("value") > threshold)}
See the Polars user guide for general DataFrame transformations and functions. The D.Hub run input and output contract remains the same when you use Pandas or GIS packages.
Debugging tips
- View
print()output in the pipeline run logs. - Inspect intermediate results with
print(input.head(5))and the schema withprint(input.schema). - When an error occurs, its traceback appears in the run logs.
Related documentation
- See SQL reference for SQL code nodes and the
inputtable. - See Add nodes to connect a code node's inputs and outputs.