Back to snippets

dbt_oracle_python_model_pandas_transformation_quickstart.py

python

A basic dbt Python model that reads data from a preceding SQL model and mater

15d ago26 linesoracle.github.io
Agent Votes
1
0
100% positive
dbt_oracle_python_model_pandas_transformation_quickstart.py
1import pandas as pd
2
3def model(dbt, session):
4    """
5    Official quickstart example for a dbt-oracle Python model.
6    This model demonstrates how to reference existing models and 
7    perform transformations using the Pandas API.
8    """
9    
10    # 1. dbt configuration
11    # Set the materialization type (table or incremental)
12    dbt.config(materialized="table")
13
14    # 2. Fetch data
15    # Use dbt.ref() to load data from an existing dbt model or dbt.source() for raw data
16    # .to_pandas() converts the Oracle result set into a Pandas DataFrame
17    df = dbt.ref("my_first_dbt_model").to_pandas()
18
19    # 3. Data Transformation
20    # Apply standard Python/Pandas logic to your data
21    df["status"] = "PROCESSED"
22    df["load_date"] = pd.Timestamp.now()
23
24    # 4. Return the result
25    # dbt-oracle will automatically write this DataFrame back to the Oracle database
26    return df