Back to .md Directory

Data Retrieval

Downloads UK electricity generation and pricing data from Electric Insights and Energy Charts APIs using the moepy library, then visualises fuel-mix time-series.

May 2, 2026
0 downloads
3 views
ai rag eval
View source

What this file does

Downloads UK electricity generation and pricing data from Electric Insights and Energy Charts APIs using the moepy library, then visualises fuel-mix time-series.

When to use it

  • You need historical GB electricity market data for analysis
  • You want to replicate the merit-order effect study from the original paper
  • You are building a data pipeline for energy system modelling
  • You need a template for batch-API data retrieval with progress bars

Assumes this stack

Pythonmoepypandasmatplotlib

Data Retrieval

Binder

This notebook outlines the retrieval of data from Electric Insights and Energy Charts using the moepy library. This data will be used in later user-guide notebooks.

<br>

Imports

from moepy import retrieval, eda
<br>

Electric Insights

To download data from all of the electric insights streams is as simple as calling get_EI_data and specifying the start and end dates. The data will be retrieved in 3 month batches as this is the maximum limit currently allowed by the API, you can change the freq parameter to adjust this.

Please save data once downloaded to avoid needless calls to the API.

start_date = '2010-01-01'
end_date = '2020-12-31'

df_EI = retrieval.get_EI_data(start_date, end_date)
df_EI.to_csv('../data/ug/electric_insights.csv')

df_EI.head()
100%|██████████████████████████████████████████████████████████████████████████████████| 45/45 [08:51<00:00, 11.81s/it]
local_datetimeday_ahead_priceSPimbalance_pricevalueSumtemperatureTCO2_per_hgCO2_per_kWhnuclearbiomasscoal...demandpumped_storagewind_onshorewind_offshorebelgiandutchfrenchirelandnorthern_irelandirish
2010-01-01 00:00:00+00:0032.91155.7755.771.1162684297.89709.902...37.948-0.435NoneNone001.96300-0.234
2010-01-01 00:30:00+00:0033.25259.8959.891.1164324307.897010.074...38.227-0.348NoneNone001.97400-0.236
2010-01-01 01:00:00+00:0032.07353.1553.151.1163184317.893010.049...37.898-0.424NoneNone001.98300-0.236
2010-01-01 01:30:00+00:0031.99438.4838.481.1157684277.89609.673...36.918-0.575NoneNone001.98300-0.236
2010-01-01 02:00:00+00:0031.47537.737.71.1152504247.909.37...35.961-0.643NoneNone001.98300-0.236
<br>

We'll visualise the time-series of output by fuel in the style of this paper, the author of which was also a creator of the Electric Insights site.

df_EI_plot = eda.clean_EI_df_for_plot(df_EI, freq='7D')

eda.stacked_fuel_plot(df_EI_plot, dpi=250)
<AxesSubplot:ylabel='Generation (GW)'>

png

<br>

Energy Charts

To download fuel generation data from the energy charts site call get_EC_data and specify the start and end dates.

As before, please save data once downloaded.

df_EC = retrieval.get_EC_data(start_date, end_date)
        
df_EC.head()
100%|████████████████████████████████████████████████████████████████████████████████| 576/576 [05:02<00:00,  1.91it/s]
local_datetimeBiomassBrown CoalGasHard CoalHydro PowerOilOthersPumped StorageSeasonal StorageSolarUraniumWindNet Balance
2010-01-04 00:00:00+01:003.63716.5334.72610.0782.331000.0520.068016.8260.635-1.229
2010-01-04 01:00:00+01:003.63716.5444.8568.8162.293000.0380.003016.8410.528-1.593
2010-01-04 02:00:00+01:003.63716.3685.2757.9542.299000.0320016.8460.616-1.378
2010-01-04 03:00:00+01:003.63715.8375.3547.6812.299000.0270016.6990.63-1.624
2010-01-04 04:00:00+01:003.63715.4525.9187.4982.3010.00300.020016.6350.713-0.731
<br>

Once again we'll visualise the long-term average output time-series separated by fuel-type

df_EC_plot = eda.clean_EC_df_for_plot(df_EC)
eda.stacked_fuel_plot(df_EC_plot, dpi=250)
<AxesSubplot:ylabel='Generation (GW)'>

png

What's inside

2 API retrieval functions, 2 data-cleaning calls, 2 stacked-area plots, 1 progress indicator, 1 CSV export

Change this for your project

  • Replace start_date = '2010-01-01' and end_date = '2020-12-31' with your date range
  • Replace '../data/ug/electric_insights.csv' with your own output path
  • Replace '../data/ug/' directory if you change the save location

Where it goes

Keep it in your repository where the agent or team that needs it will read it.

Worth borrowing

  • Batching API calls in 3-month chunks to respect API limits
  • Saving raw data to CSV immediately to avoid repeated API calls
  • Separating retrieval and visualisation into distinct functions for reusability

Related Documents