Seth Stokes
03/25/2025, 5:55 PM@subdag to a Parallelizable node?
I am trying to do the following but and missing some of the plumbing.Seth Stokes
03/25/2025, 5:55 PM@config.when(split_by_dimension=True)
@extract_columns(*
["portfolio_name"]
+ RAW_COLS_TO_EXTRACT
)
def split_trades_by_dimension(trades: pd.DataFrame) -> Parallelizable[pd.DataFrame]:
"""Group trades by dimension."""
for dim, cols in DIMENSION_SCHEMES.items():
grp = (
trades
.pipe(_create_portfolio_name_dimensions, columns=cols)
.groupby(["portfolio_name"] + cols, as_index=False, dropna=False)
)
for dimension_df in grp:
yield dimension_df
@config.when(split_by_dimension=True)
@subdag(prepare_trades_for_request_obj, external_inputs="split_trades_by_dimension")
def trades_by_dimension(portfolios_validated: Collect[pd.DataFrame]) -> pd.DataFrame:: # coming from prepare_trades_for_request_obj
"""Extract dimension data from formatted positions."""
return pd.concat(portfolios_validated, ignore_index=True)Seth Stokes
03/25/2025, 6:07 PMraise base.InvalidDecoratorException(
hamilton.function_modifiers.base.InvalidDecoratorException: Error <function split_trades_by_dimension at 0x000001AD9D53B420> does not output a type we know about. Is it a dataframe type we support?
Am I not allowed to @extract_columns of a Parallelizable node?Seth Stokes
03/25/2025, 6:30 PM@extract_columns to this intermediate node dimension_df then I get a new error.
@extract_columns(*
["portfolio_name"]
+ RAW_COLS_TO_EXTRACT
)
def dimension_df(split_trades_by_dimension: pd.DataFrame) -> pd.DataFrame:
"""Extract dimension data from formatted positions."""
return split_trades_by_dimension
Error:
File "C:\codebase\venv\Lib\site-packages\hamilton\function_modifiers\expanders.py", line 677, in extractor_fn
if column_to_extract not in df:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\codebase\venv\Lib\site-packages\pandas\core\generic.py", line 1576, in __nonzero__
raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().Seth Stokes
03/25/2025, 7:38 PMStefan Krawczyk
03/25/2025, 8:21 PMRAW_COLS_TO_EXTRACT ?Stefan Krawczyk
03/25/2025, 8:22 PMStefan Krawczyk
03/25/2025, 8:22 PMSeth Stokes
03/25/2025, 8:59 PMsubdag?
@config.when(split_by_dimension=True)
def split_trades_by_dimension(trades: pd.DataFrame) -> Parallelizable[pd.DataFrame]:
"""Group trades by dimension."""
for dim, cols in DIMENSION_SCHEMES.items():
grp = (
trades
.pipe(_create_portfolio_name_dimensions, columns=cols)
.groupby(["portfolio_name"] + cols, as_index=False, dropna=False)
)
for _, dimension_df in grp:
yield dimension_df.assign(dimension=dim)
@config.when(split_by_dimension=True)
def dimension_df(split_trades_by_dimension: pd.DataFrame) -> pd.DataFrame:
"""Extract dimension data from formatted positions."""
return split_trades_by_dimension
@config.when(split_by_dimension=True)
@extract_columns(*
["portfolio_name"]
+ RAW_COLS_TO_EXTRACT
)
def trades_by_dimension(dimension_df: Collect[pd.DataFrame]) -> pd.DataFrame:
"""Extract dimension data from formatted positions."""
return pd.concat(dimension_df, ignore_index=True)Stefan Krawczyk
03/26/2025, 1:13 AMStefan Krawczyk
03/26/2025, 1:14 AMSeth Stokes
03/26/2025, 3:14 AMextract_columns is needed for one of the load_from=*modules that I would like to apply to each dimension_dfSeth Stokes
03/27/2025, 6:30 PMsubdag that calls a dataflow with extract_columns inside it. The extract_columns must be encapsulated inside the dataflow - so I figured out that much so far. Now the challenge I am having is that there a pipe that I want applied outside of the dataflow inside of apply_subdag* to extract_columns.Stefan Krawczyk
03/28/2025, 12:58 AMdef split_trades_by_dimension(trades: pd.DataFrame) -> Parallelizable[pd.DataFrame]:
"""bookend start"""
for dim, cols in DIMENSION_SCHEMES.items():
grp = (
trades
.pipe(_create_portfolio_name_dimensions, columns=cols)
.groupby(["portfolio_name"] + cols, as_index=False, dropna=False)
)
for dimension_df in grp:
yield dimension_df
@extract_columns(*
["portfolio_name"]
+ RAW_COLS_TO_EXTRACT
)
def dimension_df(split_trades_by_dimension: pd.DataFrame) -> pd.DataFrame: # if this is important you could move this function into the subdag module, or a separate one rather than have it here
"""Extract dimension data from formatted positions."""
return split_trades_by_dimension
@subdag(prepare_trades_for_request_obj,
inputs={
input_name: source(input_name)
for input_name in ["portfolio_name", "dimension_df"] + RAW_COLS_TO_EXTRACT
}
)
def trades_by_dimension(portfolios_validated: pd.DataFrame) -> pd.DataFrame:: # coming from prepare_trades_for_request_obj
"""Extract dimension data from formatted positions."""
return portfolios_validated
def all_trades_by_dimendion(trades_by_dimension: Collect[pd.DataFrame]) -> pd.DataFrame:
# bookend end
return pd.concat(portfolios_validated, ignore_index=True)Seth Stokes
04/09/2025, 12:37 AM