-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_gas_prices.py
169 lines (107 loc) · 4.57 KB
/
daily_gas_prices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, os
import pandas as pd
from importlib import reload
from bs4 import BeautifulSoup
import urllib.request
from tqdm import tqdm
import numpy as np
import grimsel.auxiliary.timemap as timemap
import grimsel.auxiliary.aux_sql_func as aql
import PROFILE_READER.profile_reader as profile_reader
reload(profile_reader)
class DailyGasPriceReader(profile_reader.ProfileReader):
'''
'''
dict_sql_default = dict(sc='profiles_raw', tb='daily_gas_prices')
data_dir = os.path.normpath('DAILY_FUELS/REUTER_GAS')
tb_cols = [('"DateTime"', 'TIMESTAMP'),
('hub', 'VARCHAR'),
('value', 'DOUBLE PRECISION'),
('hy', 'SMALLINT'),
('year', 'SMALLINT')]
tb_pk = ['hub', 'year', 'hy']
exclude_substrings=[]
def __init__(self, kw_dict):
super().__init__(**kw_dict)
self.get_fn_list()
def read(self, fn):
df_add = pd.read_excel(fn, header=[0,1,2]).dropna().reset_index()
df_add = df_add[[c for c in df_add.columns if '€/MWh' in c or 'Date' in c]]
df_add['DateTime'] = pd.to_datetime(df_add['Date'])
df_add = df_add.drop('Date', axis=1)
df_add.columns = df_add.columns.droplevel([-1, -2])
df_add[df_add == '-'] = np.nan
df_time_map = pd.DataFrame(index=pd.date_range(df_add.DateTime.min(),
df_add.DateTime.max(),
freq='D'))
df_time_map = df_time_map.reset_index().rename(columns={'index':
'DateTime'})
df_add = df_time_map.join(df_add.set_index('DateTime'), on='DateTime')
df_add = df_add.set_index('DateTime')
df_add = df_add.fillna(np.nan)
df_add = df_add.loc[-df_add.isnull().all(axis=1)]
df_add = (df_add.stack().reset_index()
.rename(columns={'level_1': 'hub', 0: 'value'}))
df_add = df_add.pivot_table(index='DateTime', values='value', aggfunc=np.mean).reset_index()
df_add['hub'] = 'mean_3_hubs'
df_add['DateTime'] = df_add.DateTime.dt.tz_localize('UTC')
tm = timemap.TimeMap(keep_datetime=True)
tm.gen_hoy_timemap(freq='D', start=df_add.DateTime.min(), stop=df_add.DateTime.max())
df_tm = tm.df_time_map.copy()
df_tm['DateTime'] = pd.to_datetime(df_tm.DateTime.dt.date)
df_tm['DateTime'] = df_tm.DateTime.dt.tz_localize('UTC')
df_add_1 = pd.merge(df_tm, df_add, on='DateTime', how='left')
df_add_1 = df_add_1.sort_values('DateTime')
df_add_1['value'] = df_add_1['value'].fillna(method='ffill')
df_add_1['hub'] = df_add_1['hub'].fillna(method='ffill')
return df_add_1[['DateTime', 'value', 'hub']]
class QuandlCoalPriceReader(profile_reader.ProfileReader):
'''
'''
dict_sql_default = dict(sc='profiles_raw', tb='daily_coal_prices')
data_dir = os.path.normpath('DAILY_FUELS/QUANDL_ROTTERDAM')
tb_cols = [('"DateTime"', 'TIMESTAMP'),
('index', 'VARCHAR'),
('value', 'DOUBLE PRECISION'),
('hy', 'SMALLINT'),
('mt_id', 'SMALLINT'),
('year', 'SMALLINT')]
tb_pk = ['index', 'year', 'hy']
exclude_substrings=[]
def __init__(self, kw_dict):
super().__init__(**kw_dict)
self.get_fn_list()
def read(self, fn):
df_add = pd.read_csv(fn)
df_add['DateTime'] = pd.to_datetime(df_add.Date)
df_add = (df_add.set_index('DateTime')['Settle']
.rename('value').reset_index())
df_add['index'] = (fn.split(os.path.sep)[-1]
.split('_')[-1]
.split('.')[0])
df_add['DateTime'] = df_add.DateTime.dt.tz_localize('UTC')
return df_add
def post_processing(self, df):
df['mt_id'] = df.DateTime.dt.month - 1
return df
if __name__ == '__main__':
sys.exit()
kw_dict = dict(dict_sql=dict(db='storage2'),
exclude_substrings=[],
tm_filt={'year': range(2005, 2018)},
ext=['csv'])
op = QuandlCoalPriceReader(kw_dict)
self = op
fn = self.fn_list[0]
op.read_all()
kw_dict = dict(dict_sql=dict(db='storage2'),
exclude_substrings=[],
tm_filt={'year': range(2005, 2018)},
ext=['xlsx'])
op = DailyGasPriceReader(kw_dict)
self = op
fn = self.fn_list[0]
op.read_all()
# %%