@@ -3343,15 +3343,45 @@ def apply_final_fixup(
33433343 df_list .append (stock_rows )
33443344
33453345 df = pd .concat (df_list )
3346+ # Make IRE_FLO out of EFF for IRE processes
3347+ ire_set = set (model .processes ["process" ][model .processes ["sets" ] == "IRE" ])
3348+ # Calculate index of IRE processes for which EFF is specified
3349+ i_ire_eff = (df ["original_attr" ] == "EFF" ) & df ["process" ].isin (ire_set )
3350+ if any (i_ire_eff ):
3351+ df .loc [i_ire_eff , "attribute" ] = "IRE_FLO"
3352+ ire_flows = (
3353+ model .trade [["process" , "origin" , "destination" , "in" , "out" ]]
3354+ .drop_duplicates ()
3355+ .copy ()
3356+ )
3357+ ire_flows .rename (columns = {"origin" : "region" }, inplace = True )
3358+ # Keep only those flows that are relevant
3359+ ire_flows = df [["region" , "process" ]][i_ire_eff ].merge (ire_flows , how = "inner" )
3360+ # Perform an outer merge to include the data for IRE_FLO
3361+ df = df .merge (ire_flows , how = "outer" )
3362+ # Create the region2 and commodity2 columns if they do not exist
3363+ for col in {"region2" , "commodity2" }.difference (df .columns ):
3364+ df [col ] = pd .NA
3365+ # Recalculate the index of IRE processes for which EFF is specified
3366+ i_ire_eff = (df ["original_attr" ] == "EFF" ) & (df ["attribute" ] == "IRE_FLO" )
3367+ # Replace the values in the region2, commodity and commodity2 columns
3368+ for k , v in {
3369+ "destination" : "region2" ,
3370+ "out" : "commodity2" ,
3371+ "in" : "commodity" ,
3372+ }.items ():
3373+ df .loc [i_ire_eff , v ] = df .loc [i_ire_eff , k ]
33463374
33473375 # Clean up
33483376 # TODO: Do this comprehensively for all relevant tables
33493377 # TODO: Duplicates should only be removed if in the same file/module
33503378 keep_cols = {
33513379 "attribute" ,
33523380 "region" ,
3381+ "region2" ,
33533382 "process" ,
33543383 "commodity" ,
3384+ "commodity2" ,
33553385 "other_indexes" ,
33563386 "cg" ,
33573387 "year" ,
@@ -3367,7 +3397,9 @@ def apply_final_fixup(
33673397 df .dropna (subset = "value" , inplace = True )
33683398 drop_cols = [col for col in df .columns if col != "value" and col not in keep_cols ]
33693399 df .drop (columns = drop_cols , inplace = True )
3370- df = df .drop_duplicates (subset = list (keep_cols ), keep = "last" )
3400+ df = df .drop_duplicates (
3401+ subset = list (keep_cols .intersection (df .columns )), keep = "last"
3402+ )
33713403
33723404 # Control application of i/e rules from syssettings
33733405 if not config .ie_override_in_syssettings :
@@ -3379,8 +3411,8 @@ def apply_final_fixup(
33793411 duplicated = df [i ].duplicated (
33803412 subset = [
33813413 col
3382- for col in keep_cols
3383- if col != "value" and col not in { "module_name" , "module_type" }
3414+ for col in keep_cols . intersection ( df . columns ). difference ( dm_cols )
3415+ if col != "value"
33843416 ],
33853417 keep = False ,
33863418 )
@@ -3399,7 +3431,9 @@ def apply_final_fixup(
33993431 col for col in df .columns if col != "value" and col not in keep_cols
34003432 ]
34013433 df .drop (columns = drop_cols , inplace = True )
3402- df = df .drop_duplicates (subset = list (keep_cols ), keep = "last" )
3434+ df = df .drop_duplicates (
3435+ subset = list (keep_cols .intersection (df .columns )), keep = "last"
3436+ )
34033437 tables [Tag .uc_t ] = df .reset_index (drop = True )
34043438
34053439 return tables
0 commit comments