@@ -254,18 +254,18 @@ def __method_none(self, bids, offers, pricing_method):
254254 """Clears the market with no clearing method, i.e. only the retailer acts as trading partner"""
255255
256256 # Merge bids and offers on the energy_cumsum column
257- bids_offers = bids . join ( offers , on = C_ENERGY_CUMSUM , how = 'outer ' )
257+ bids_offers = pl . concat ([ bids , offers ] , how = 'diagonal ' )
258258
259259 # Sort the bids and offers by the energy_cumsum
260260 bids_offers = bids_offers .sort (C_ENERGY_CUMSUM , descending = False )
261261
262- # Remove all columns that end on _right
263- double_cols = [ col for col in bids_offers .columns if col . endswith ( '_right' )]
264- for col in double_cols :
265- orig_col = col . rsplit ( '_' , 1 )[ 0 ]
266- bids_offers = bids_offers . with_columns (pl .coalesce ([ orig_col , col ]). alias ( orig_col ))
267- bids_offers = bids_offers . drop ( double_cols )
268-
262+ # Fill the NaN values with the retailer
263+ bids_offers = bids_offers .with_columns ([
264+ pl . when ( pl . col ( c . TC_ID_AGENT_IN ). is_not_null ()). then ( pl . col ( c . TC_ID_AGENT_IN ))
265+ . otherwise ( pl . lit ( 'retailer' )). alias ( c . TC_ID_AGENT_IN ),
266+ pl . when (pl .col ( c . TC_ID_AGENT_OUT ). is_not_null ()). then ( pl . col ( c . TC_ID_AGENT_OUT ))
267+ . otherwise ( pl . lit ( 'retailer' )). alias ( c . TC_ID_AGENT_OUT ),
268+ ])
269269
270270 # Create bids and offers table where retailer is the only trading partner
271271 # Note: This currently works only for one retailer named 'retailer' in the future it needs to first obtain the
@@ -277,6 +277,10 @@ def __method_none(self, bids, offers, pricing_method):
277277 bids = bids .fill_null (strategy = 'backward' )
278278 offers = offers .fill_null (strategy = 'backward' )
279279
280+ # Filter rows that have the same agent id in the in and out column (cannot trade with themselves)
281+ bids = bids .filter (pl .col (c .TC_ID_AGENT_IN ) != pl .col (c .TC_ID_AGENT_OUT ))
282+ offers = offers .filter (pl .col (c .TC_ID_AGENT_IN ) != pl .col (c .TC_ID_AGENT_OUT ))
283+
280284 # Clear bids and offers
281285 bids_cleared = bids .filter (pl .col (c .TC_PRICE_PU_IN ) >= pl .col (c .TC_PRICE_PU_OUT ))
282286 offers_cleared = offers .filter (pl .col (c .TC_PRICE_PU_IN ) >= pl .col (c .TC_PRICE_PU_OUT ))
@@ -288,7 +292,6 @@ def __method_none(self, bids, offers, pricing_method):
288292 # Create new dataframe with the cleared bids and offers
289293 trades_cleared = pl .concat ([bids_cleared , offers_cleared ], how = 'vertical' )
290294
291-
292295 # Calculate the price and energy of the trades
293296 trades_cleared = trades_cleared .with_columns (
294297 (trades_cleared .select ([c .TC_ENERGY_IN , c .TC_ENERGY_OUT ]).min (axis = 1 ).alias (c .TC_ENERGY )),
@@ -321,6 +324,7 @@ def __create_bids_offers(self, include_retailer=True):
321324 retailer = retailer .with_columns (
322325 [
323326 pl .col (c .TC_TIMESTAMP ).alias (c .TC_TIMESTEP ),
327+ pl .lit (self .bids_offers .select (pl .first (c .TC_TIMESTAMP ))).alias (c .TC_TIMESTAMP ),
324328 pl .lit (None ).alias (c .TC_ENERGY_TYPE ),
325329 # TODO: This can be removed once the energy type is added to the retailer table
326330 ]
@@ -332,6 +336,8 @@ def __create_bids_offers(self, include_retailer=True):
332336
333337 retailer = retailer .with_columns (
334338 [
339+ pl .col (c .TC_TIMESTAMP ).cast (pl .Datetime (time_unit = 'ns' , time_zone = 'UTC' ), strict = False ),
340+ pl .col (c .TC_TIMESTEP ).cast (pl .Datetime (time_unit = 'ns' , time_zone = 'UTC' ), strict = False ),
335341 pl .col (c .TC_REGION ).cast (pl .Categorical , strict = False ),
336342 pl .col (c .TC_MARKET ).cast (pl .Categorical , strict = False ),
337343 pl .col (c .TC_NAME ).cast (pl .Categorical , strict = False ),
@@ -691,6 +697,10 @@ def __apply_grid_levies(self, transactions):
691697 # Concat the grids and levies
692698 transactions = pl .concat ([grid , levies ], how = 'align' )
693699
700+ # Set the timestamp column to the current timestamp
701+ transactions = transactions .with_columns (pl .lit (self .tasks [c .TC_TIMESTAMP ]).alias (c .TC_TIMESTAMP )
702+ .cast (pl .Datetime (time_unit = 'ns' , time_zone = 'UTC' )))
703+
694704 return transactions
695705
696706 def __method_pda (self , bids , offers , pricing_method ):
0 commit comments