Seth Stokes
12/03/2024, 9:27 PMfiltered_df.with_rename_columns
@pipe_input(
step(_rename_columns, mapping=NORMALIZED_MAPPING),
step(_filter_out_noise, diff_col="Amount Diff").when_in(process_id=["a", "b", "c", "d"]),
)
def filtered_df(raw: pd.DataFrame) -> pd.DataFrame:
return raw
Am I missing something?Stefan Krawczyk
12/03/2024, 9:34 PMSeth Stokes
12/03/2024, 9:51 PMStefan Krawczyk
12/03/2024, 9:59 PM%%cell_to_module pipe_example --display
from hamilton.function_modifiers import pipe_input, step
import pandas as pd
NORMALIZED_MAPPING = {"old_name": "new_name"}
def _rename_columns(df: pd.DataFrame, mapping: dict) -> pd.DataFrame:
return df.rename(columns={"old_name": "new_name"})
def _filter_out_noise(df: pd.DataFrame, diff_col: str) -> pd.DataFrame:
return df[df[diff_col] > 0]
@pipe_input(
step(_rename_columns, mapping=NORMALIZED_MAPPING),
step(_filter_out_noise, diff_col="Amount Diff").when_in(process_id=["a", "b", "c", "d"]),
)
def filtered_df(raw: pd.DataFrame) -> pd.DataFrame:
return rawSeth Stokes
12/03/2024, 10:05 PMfiltered_df is now apparently absent from my dag.Seth Stokes
12/03/2024, 10:17 PM# transforms
@pipe_input(
step(_rename_columns, mapping=NORMALIZED_MAPPING),
step(_filter_out_noise, diff_col="Amount Diff").when_in(process_id=["a", "b", "c", "d"]),
)
def filtered_df(raw: pd.DataFrame) -> pd.DataFrame:
return raw
@hamilton_exclude
def all_processes_table(process_ids: List[Literal["a","b","c","d"]]) -> pd.DataFrame:
import __main__
from hamilton import driver, base
builder = (
driver.Builder()
.with_modules(__main__)
.with_adapters(base.PandasDataFrameResult())
)
ALL_DFS= []
for _id in process_ids:
builder_copy = builder.copy()
dr = builder_copy.with_config({"process_id": _id}).build()
df = dr.execute(["filtered_df"], inputs={"save_location": save_location})
ALL_DFS.append(df)
return pd.concat(ADJUSTMENT_DFS, ignore_index=True)
The problem was __main__ when trying to use all_processes_table in more_dataflow
# more_dataflow.py
from transforms import all_processes_table
all_processes_table(process_ids=list("abcd"))Seth Stokes
12/03/2024, 10:33 PM_rename_columns step be applied if the when_in condition failed?Stefan Krawczyk
12/03/2024, 10:33 PMwould onlyno, itโs a separate independent step._rename_columnsbe applied if thestepcondition failed?when_in
Seth Stokes
12/03/2024, 10:36 PM_rename_columns step result return raw in the even the sonditional subsequent step fails to be met?Seth Stokes
12/03/2024, 10:37 PMraw -> _rename_columns -> filtered_df - condtion not met for _filter_out_noise
2. raw -> _rename_columns -> _filter_out_noise -> filtered_df - condition met for _filter_out_noiseStefan Krawczyk
12/03/2024, 10:38 PMSeth Stokes
12/03/2024, 10:39 PM__main__ issue ? Thats a good POC workflow, but a bad practice for what I was trying to do?Stefan Krawczyk
12/03/2024, 10:44 PMall_processes_table from another module, __main__ will be different than the one it is in. E.g.
print(__main__)
in that function.