Slackbot
01/30/2023, 11:30 PMStefan Krawczyk
01/30/2023, 11:38 PMdr.execute(['merge_prod_w_state_map'])
Option (2):
Add the @extract_columns
decorator to merge_prod_w_state_map
from hamilton.function_modifiers import extract_columns
...
@extract_columns(*['State_Area', 'month_a', 'month_b', 'pct_chg', 'State', 'Standard','Postal'])
def merge_prod_w_state_map(load_production: pd.DataFrame,
load_mapping: pd.DataFrame
) -> pd.DataFrame:
...
and then
dr.execute(["State"])
should work.Seth Stokes
01/30/2023, 11:43 PMStefan Krawczyk
01/30/2023, 11:44 PMdef State(merge_prod_w_state_map: pd.Dataframe) -> pd.Series:
return merge_prod_w_state_map["State"]
...
Stefan Krawczyk
01/30/2023, 11:46 PMSeth Stokes
01/30/2023, 11:49 PMStefan Krawczyk
01/30/2023, 11:58 PMStefan Krawczyk
01/31/2023, 12:16 AMSeth Stokes
02/01/2023, 11:34 PM@parameterize
could come into play? So that I would only need to define the function once and it would then do the transform on a list of columns?
# explicit
df.assign(C1_PCT_CHG=lambda x: x.C1.pct_change(),
C2_PCT_CHG=lambda x: x.C2.pct_change(),
C3_PCT_CHG=lambda x: x.C3.pct_change(),
...
CN_PCT_CHG=lambda x: x.CN.pct_change()
)
# loop that fails due to scope. (this cannot be extracted to a function and directly call df since .unstack() was called just before.)
df.assign(**{f'{col}_PCT_CHG': lambda x: x[col].pct_change() for col in ('c1', 'c2', 'c3'...cN)})
Stefan Krawczyk
02/01/2023, 11:58 PMCOLUMN_LIST = ('c1', 'c2', 'c3'...'cN')
@parameterize_sources(**{f"{c}_PCT_CHG": {"col": c} for c in COLUMN_LIST})
def pct_change(col: pd.Series) -> pd.Series:
return col.pct_change()
docs for parameterize_sources.Stefan Krawczyk
02/02/2023, 12:03 AM# explicit
df.assign(C1_PCT_CHG=df.C1.pct_change(),
C2_PCT_CHG=df.C2.pct_change(),
C3_PCT_CHG=df.C3.pct_change(),
...
CN_PCT_CHG=df.CN.pct_change()
)
# in a comprehension
df.assign(**{f'{col}_PCT_CHG': df[col].pct_change() for col in ('c1', 'c2', 'c3'...cN)})
should work. (no need for the lambda)Seth Stokes
02/02/2023, 12:09 AM