Skip to content

Commit

Permalink
Merge pull request #7 from JotaFan/main
Browse files Browse the repository at this point in the history
Simplify Dataframe Construction
  • Loading branch information
SanPen authored Aug 18, 2023
2 parents 25e348e + cbbe1b1 commit 82f6cef
Show file tree
Hide file tree
Showing 4 changed files with 729 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*__pycache__*
.ipynb*
.idea
*.pkl
*.pickle
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

21 changes: 4 additions & 17 deletions ESIOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def get_names(self, indicators_list):
for i in indicators_list:
names.append(self.__indicators_name__[i])

return np.array(names, dtype=np.object)
return np.array(names)

def save_indicators_table(self, fname='indicadores.xlsx'):
"""
Expand All @@ -200,16 +200,14 @@ def __get_query_json__(self, indicator, start_str, end_str):

# https://www.esios.ree.es/es/analisis/1293?vis=2&start_date=21-06-2016T00%3A00&end_date=21-06-2016T23%3A50&compare_start_date=20-06-2016T00%3A00&groupby=minutes10&compare_indicators=545,544#JSON
url = 'https://api.esios.ree.es/indicators/' + indicator + '?start_date=' + start_str + '&end_date=' + end_str

# Perform the call
result = None
req = urllib.request.Request(url, headers=self.__get_headers__())
with urllib.request.urlopen(req) as response:
try:
json_data = response.read().decode('utf-8')
except:
json_data = response.readall().decode('utf-8')
result = json.loads(json_data)

return result

def get_data(self, indicator, start, end):
Expand Down Expand Up @@ -239,18 +237,8 @@ def get_data(self, indicator, start, end):

# transform the data
d = result['indicator']['values'] # dictionary of values

if len(d) > 0:
hdr = list(d[0].keys()) # headers
data = np.empty((len(d), len(hdr)), dtype=np.object)

for i in range(len(d)): # iterate the data entries
for j in range(len(hdr)): # iterate the headers
h = hdr[j]
val = d[i][h]
data[i, j] = val

df = pd.DataFrame(data=data, columns=hdr) # make the DataFrame
df = pd.DataFrame(d)

df['datetime_utc'] = pd.to_datetime(df['datetime_utc']) # convert to datetime

Expand Down Expand Up @@ -312,12 +300,11 @@ def merge_series(df_list, names, pandas_sampling_interval='1H'):
# print(name)

if df is not None:

if name == 'Precio mercado SPOT Diario':
df = df[df.geo_id == 3] # pick spain only

dfp = df[[name]].astype(float) # .resample(pandas_sampling_interval).pad()
# dfp2 = pd.DataFrame(data=dfp.values, index=dfp.index, columns=[name])

if merged_df is None:
merged_df = dfp
else:
Expand Down
Loading

0 comments on commit 82f6cef

Please sign in to comment.