{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plot Germany's electricity price\n", "\n", "Power production from https://www.energy-charts.de/power/month_2015_04.json\n", "\n", "Electricity price from http://www.pfbach.dk/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import print_function, division\n", "\n", "import pandas as pd, urllib, os, json\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "import datetime as dt\n", "\n", "data_folder = \"data\"\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def duration_curve(s,percent=False):\n", " if type(s) is not pd.Series:\n", " print(\"Warning, {} is not a pandas Series!\".format(s))\n", " return\n", " r = s.sort_values(ascending=False)\n", " l = len(s)\n", " norm = 100. if percent else 1.\n", " r.index=[norm*i/float(l) for i in range(l)]\n", " return r" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def pad_number(number,digits):\n", " st = str(number)\n", " n_missing = digits - len(st)\n", " if n_missing > 0:\n", " return \"0\"*n_missing + st\n", " else:\n", " return st" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'012'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pad_number(12,3)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ "years = [2015]\n", "\n", "datas = {}\n", "\n", "for year in years:\n", " datas[year] = {}\n", " for month in range(1,13):\n", " file_name = \"month_{}_{}.json\".format(year,pad_number(month,2))\n", " path_name = os.path.join(data_folder,file_name)\n", " if not os.path.isfile(path_name):\n", " print(\"Downloading\",file_name)\n", " url = \"https://www.energy-charts.de/power/\" + file_name\n", " urllib.urlretrieve (url, path_name)\n", " with open(path_name) as data_file: \n", " datas[year][month] = json.load(data_file)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "columns = [d[\"key\"][0][\"en\"] for d in datas[years[0]][1]]\n", "data = []\n", "\n", "for year in years:\n", " for month in range(1,13):\n", " for i in range(len(columns)):\n", " if datas[year][month][i][\"key\"][0][\"en\"] != columns[i]:\n", " print(\"Error\")\n", " for j in range(len(datas[year][month][0][\"values\"])):\n", " data.append([d[\"values\"][j][1] for d in datas[year][month]])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "index = pd.date_range(\"2015-01-01 00:00\",\"2015-12-31 23:00\",freq=\"H\")\n", "\n", "df = pd.DataFrame(data=data,\n", " columns=columns,\n", " index=index)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Imports | \n", "Hydro | \n", "Biomass | \n", "Nuclear | \n", "Brown Coal | \n", "Hard Coal | \n", "Gas | \n", "Oil | \n", "Others | \n", "Pumped Storage | \n", "Seasonal Storage | \n", "Wind | \n", "Solar | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "8760.000000 | \n", "
mean | \n", "-5.348893 | \n", "2.467432 | \n", "5.346180 | \n", "10.048257 | \n", "15.992861 | \n", "11.618610 | \n", "3.763840 | \n", "0.259309 | \n", "0.090096 | \n", "1.049443 | \n", "0.181957 | \n", "8.934102 | \n", "3.820646 | \n", "
std | \n", "2.962256 | \n", "0.624834 | \n", "0.387098 | \n", "1.387940 | \n", "2.521052 | \n", "5.728519 | \n", "2.088773 | \n", "0.150649 | \n", "0.070991 | \n", "0.933206 | \n", "0.150769 | \n", "7.139880 | \n", "5.829201 | \n", "
min | \n", "-15.862000 | \n", "0.443000 | \n", "4.786000 | \n", "5.727000 | \n", "6.845000 | \n", "1.621000 | \n", "0.789000 | \n", "0.000000 | \n", "0.006000 | \n", "0.008000 | \n", "0.000000 | \n", "0.123000 | \n", "0.000000 | \n", "
25% | \n", "-7.308000 | \n", "2.007000 | \n", "4.853000 | \n", "9.465000 | \n", "14.503750 | \n", "6.194250 | \n", "2.199750 | \n", "0.201000 | \n", "0.055000 | \n", "0.384000 | \n", "0.059000 | \n", "3.468000 | \n", "0.000000 | \n", "
50% | \n", "-5.412500 | \n", "2.416000 | \n", "5.303000 | \n", "10.435000 | \n", "16.368500 | \n", "12.061000 | \n", "3.239500 | \n", "0.220000 | \n", "0.066000 | \n", "0.691500 | \n", "0.140000 | \n", "6.724000 | \n", "0.126500 | \n", "
75% | \n", "-3.488750 | \n", "2.937000 | \n", "5.599000 | \n", "10.786000 | \n", "17.979250 | \n", "16.749250 | \n", "4.548000 | \n", "0.229000 | \n", "0.096000 | \n", "1.432000 | \n", "0.274000 | \n", "12.938750 | \n", "6.103500 | \n", "
max | \n", "5.969000 | \n", "4.091000 | \n", "5.925000 | \n", "12.077000 | \n", "19.979000 | \n", "22.190000 | \n", "14.062000 | \n", "1.408000 | \n", "0.421000 | \n", "5.909000 | \n", "0.651000 | \n", "32.609000 | \n", "25.812000 | \n", "