@@ -231,6 +231,10 @@ def _convert_to_60min_interval(rawData):
231
231
# determining how many rows need to be combined to get data in 60 min format.
232
232
groupingFactor = int (60 / duration )
233
233
oldData = rawData ["data" ]
234
+ # check if there is enough data to convert to 60 min
235
+ if (len (oldData ) < groupingFactor ):
236
+ raise ValueError ("Data cannot be converted into 60 min interval since there is inadequate number of rows in the data" )
237
+
234
238
oldData ["startTimeUTC" ] = pd .to_datetime (oldData ["startTimeUTC" ])
235
239
start_time = oldData ["startTimeUTC" ].min ()
236
240
end_time = oldData ["startTimeUTC" ].max ()
@@ -256,6 +260,15 @@ def _convert_date_to_entsoe_format(dt: datetime):
256
260
return dt .replace (minute = 0 , second = 0 , microsecond = 0 ).strftime ("%Y%m%d%H%M" )
257
261
258
262
263
+ def _format_energy_data (df ):
264
+ start_time_column = df .pop ("startTimeUTC" )
265
+ df .insert (0 , "startTime" , start_time_column )
266
+ local_timezone = datetime .now ().astimezone ().tzinfo
267
+ df ["startTime" ] = pd .to_datetime (df ["startTime" ], format = "%Y%m%d%H%M" ).dt .tz_localize ("UTC" ).dt .tz_convert (local_timezone )
268
+ df .insert (1 , "startTimeUTC" , start_time_column )
269
+ return df
270
+
271
+
259
272
# the main methods
260
273
261
274
@@ -295,13 +308,14 @@ def get_actual_production_percentage(country, start, end, interval60=False) -> d
295
308
raise ValueError ("Invalid end date. Generation data is only available for the past and not the future. Use the forecast API instead" )
296
309
# this is not allowed because the entsoe-py returns error if it's greater than the present
297
310
#warnings.warn("End date is in the future. Will fetch data only till the present")
298
-
311
+
299
312
options = {
300
313
"country" : country ,
301
- "start" : start ,
302
- "end" : end ,
314
+ "start" : start . replace ( minute = 0 , second = 0 ) ,
315
+ "end" : end . replace ( second = 0 , minute = 0 ) ,
303
316
"interval60" : interval60 ,
304
317
}
318
+ # print(options)
305
319
# get actual generation data per production type and convert it into 60 min interval if required
306
320
totalRaw = _entsoe_get_actual_generation (options )
307
321
total = totalRaw ["data" ]
@@ -354,7 +368,7 @@ def get_actual_production_percentage(country, start, end, interval60=False) -> d
354
368
table [fieldName ] = table [fieldName ].astype (int )
355
369
356
370
return {
357
- "data" : table ,
371
+ "data" : _format_energy_data ( table ) ,
358
372
"data_available" : True ,
359
373
"time_interval" : duration ,
360
374
}
@@ -424,7 +438,7 @@ def get_forecast_percent_renewable(
424
438
windsolar ["startTimeUTC" ], format = "%Y%m%d%H%M"
425
439
)
426
440
windsolar ["posix_timestamp" ] = windsolar ["startTimeUTC" ].astype (int ) // 10 ** 9
427
- return {"data" : windsolar , "data_available" : True , "time_interval" : 60 }
441
+ return {"data" : _format_energy_data ( windsolar ) , "data_available" : True , "time_interval" : 60 }
428
442
except Exception as e :
429
443
print (e )
430
444
print (traceback .format_exc ())
0 commit comments