@@ -35,6 +35,7 @@ def quantity(
3535from muse .registration import registrator
3636from muse .sectors import AbstractSector
3737from muse .timeslices import QuantityType , convert_timeslice , drop_timeslice
38+ from muse .utilities import multiindex_to_coords
3839
3940OUTPUT_QUANTITY_SIGNATURE = Callable [
4041 [xr .Dataset , list [AbstractSector ], KwArg (Any )], Union [xr .DataArray , pd .DataFrame ]
@@ -396,26 +397,16 @@ def sector_supply(sector: AbstractSector, market: xr.Dataset, **kwargs) -> pd.Da
396397 data_agent ["category" ] = a .category
397398 data_agent ["sector" ] = getattr (sector , "name" , "unnamed" )
398399
399- a = data_agent .to_dataframe ("supply" )
400+ a = multiindex_to_coords ( data_agent , "timeslice" ) .to_dataframe ("supply" )
400401 a ["comm_usage" ] = a ["comm_usage" ].apply (lambda x : x .name )
401- if len (a ) > 0 and len (a .technology .values ) > 0 :
402- b = a .drop (
403- ["month" , "day" , "hour" ], axis = 1 , errors = "ignore"
404- ).reset_index ()
405- b = b [b ["supply" ] != 0 ]
406- data_sector .append (b )
407- if len (data_sector ) > 0 :
408- output = pd .concat ([u for u in data_sector ], sort = True )
402+ if not a .empty :
403+ data_sector .append (a [a ["supply" ] != 0 ])
409404
405+ if len (data_sector ) > 0 :
406+ output = pd .concat (data_sector , sort = True ).reset_index ()
410407 else :
411408 output = pd .DataFrame ()
412-
413- # Combine timeslice columns into a single column, if present
414- if "hour" in output .columns :
415- output ["timeslice" ] = list (zip (output ["month" ], output ["day" ], output ["hour" ]))
416- output = output .drop (["month" , "day" , "hour" ], axis = 1 )
417-
418- return output .reset_index ()
409+ return output
419410
420411
421412@register_output_quantity (name = ["yearly_supply" ])
@@ -572,12 +563,9 @@ def capacity(agents):
572563 data_sector .append (b )
573564
574565 if len (data_sector ) > 0 :
575- output = pd .concat ([u for u in data_sector ], sort = True )
576-
566+ output = pd .concat (data_sector , sort = True ).reset_index ()
577567 else :
578568 output = pd .DataFrame ()
579- output = output .reset_index ()
580-
581569 return output
582570
583571
@@ -643,26 +631,19 @@ def sector_consumption(
643631 data_agent ["agent" ] = a .name
644632 data_agent ["category" ] = a .category
645633 data_agent ["sector" ] = getattr (sector , "name" , "unnamed" )
646- a = data_agent .to_dataframe ("consumption" )
634+
635+ a = multiindex_to_coords (data_agent , "timeslice" ).to_dataframe (
636+ "consumption"
637+ )
647638 a ["comm_usage" ] = a ["comm_usage" ].apply (lambda x : x .name )
648- if len (a ) > 0 and len (a .technology .values ) > 0 :
649- b = a .drop (
650- ["month" , "day" , "hour" ], axis = 1 , errors = "ignore"
651- ).reset_index ()
652- b = b [b ["consumption" ] != 0 ]
653- data_sector .append (b )
654- if len (data_sector ) > 0 :
655- output = pd .concat ([u for u in data_sector ], sort = True )
639+ if not a .empty :
640+ data_sector .append (a [a ["consumption" ] != 0 ])
656641
642+ if len (data_sector ) > 0 :
643+ output = pd .concat (data_sector , sort = True ).reset_index ()
657644 else :
658645 output = pd .DataFrame ()
659-
660- # Combine timeslice columns into a single column, if present
661- if "hour" in output .columns :
662- output ["timeslice" ] = list (zip (output ["month" ], output ["day" ], output ["hour" ]))
663- output = output .drop (["month" , "day" , "hour" ], axis = 1 )
664-
665- return output .reset_index ()
646+ return output
666647
667648
668649@register_output_quantity (name = ["yearly_consumption" ])
@@ -729,12 +710,9 @@ def sectory_consumption(
729710 b = b [b ["consumption" ] != 0 ]
730711 data_sector .append (b )
731712 if len (data_sector ) > 0 :
732- output = pd .concat ([u for u in data_sector ], sort = True )
733-
713+ output = pd .concat (data_sector , sort = True ).reset_index ()
734714 else :
735715 output = pd .DataFrame ()
736- output = output .reset_index ()
737-
738716 return output
739717
740718
@@ -848,12 +826,9 @@ def sector_capital_costs(
848826 b = b [b ["capital_costs" ] != 0 ]
849827 data_sector .append (b )
850828 if len (data_sector ) > 0 :
851- output = pd .concat ([u for u in data_sector ], sort = True )
852- output = output .reset_index ()
853-
829+ output = pd .concat (data_sector , sort = True ).reset_index ()
854830 else :
855831 output = pd .DataFrame ()
856-
857832 return output
858833
859834
0 commit comments