python-esios 2.3.0__py3-none-any.whl → 2.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,8 +60,8 @@ print(sheet.frequency) # "hourly" or "hourly-quarterly"
60
60
 
61
61
  | ID | Name | Description | Geos |
62
62
  |----|------|-------------|------|
63
- | 600 | Precio mercado spot | OMIE spot market price | ES, PT, FR, DE, BE, NL |
64
- | 1001 | Precio mercado diario | Day-ahead market price | ES |
63
+ | 600 | Precio mercado SPOT Diario | OMIE spot / day-ahead market price | ES, PT, FR, DE, BE, NL |
64
+ | 1001 | PVPC T. 2.0TD | Término de facturación de energía activa del PVPC (voluntary price for small consumers) | Península, Canarias, Baleares, Ceuta, Melilla |
65
65
  | 10033 | Demanda real | Real-time electricity demand | ES |
66
66
  | 10034 | Generación eólica | Real-time wind generation | ES |
67
67
  | 10035 | Generación solar FV | Real-time solar PV generation | ES |
esios/constants.py CHANGED
@@ -14,7 +14,17 @@ MAX_RETRIES = 3
14
14
  RETRY_MIN_WAIT = 2 # seconds
15
15
  RETRY_MAX_WAIT = 10 # seconds
16
16
 
17
- # ESIOS API limits responses to ~3 weeks of data per request
18
- CHUNK_SIZE_DAYS = 21
17
+ # ESIOS API chunk sizes for historical data fetching.
18
+ # High-geo indicators (40+ geos) timeout (504) at >21 days.
19
+ # Low-geo indicators handle 6+ months per request in <0.1s.
20
+ CHUNK_SIZE_DAYS = 21 # Legacy default, kept for backward compat
21
+ CHUNK_SIZE_DAYS_LOW_GEO = 180 # 6 months for indicators with few geos
22
+ CHUNK_SIZE_DAYS_HIGH_GEO = 21 # Conservative for indicators with many geos
23
+ HIGH_GEO_THRESHOLD = 15 # Indicators with >= this many geos use smaller chunks
24
+
25
+ # Concurrent chunk fetching within a single indicator.
26
+ # 4 workers gives ~17-95x speedup over sequential with no errors.
27
+ # Diminishing returns past 4 (ESIOS server becomes the bottleneck).
28
+ DEFAULT_CHUNK_WORKERS = 4
19
29
 
20
30
  TIMEZONE = "Europe/Madrid"
@@ -3,13 +3,20 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
+ from concurrent.futures import ThreadPoolExecutor, as_completed
6
7
  from datetime import timedelta
7
8
  from typing import Any
8
9
 
9
10
  import pandas as pd
10
11
 
11
12
  from esios.cache import CacheStore
12
- from esios.constants import CHUNK_SIZE_DAYS, TIMEZONE
13
+ from esios.constants import (
14
+ CHUNK_SIZE_DAYS_HIGH_GEO,
15
+ CHUNK_SIZE_DAYS_LOW_GEO,
16
+ DEFAULT_CHUNK_WORKERS,
17
+ HIGH_GEO_THRESHOLD,
18
+ TIMEZONE,
19
+ )
13
20
  from esios.managers.base import BaseManager
14
21
  from esios.models.indicator import Indicator
15
22
  from esios.processing.dataframes import to_dataframe
@@ -131,6 +138,88 @@ class IndicatorHandle:
131
138
  f"Available: {', '.join(available)}"
132
139
  )
133
140
 
141
+ @property
142
+ def _chunk_days(self) -> int:
143
+ """Choose chunk size based on indicator's geo count.
144
+
145
+ ESIOS API times out (504) for high-geo indicators (40+ geos) with
146
+ windows larger than ~3 weeks. Low-geo indicators handle 6+ months
147
+ per request in <0.1s.
148
+
149
+ When geos are unknown (empty metadata), uses the conservative
150
+ chunk size to avoid timeouts on first fetch.
151
+ """
152
+ geo_count = len(self.geos)
153
+ if geo_count == 0:
154
+ # Unknown geo count — be conservative
155
+ return CHUNK_SIZE_DAYS_HIGH_GEO
156
+ if geo_count >= HIGH_GEO_THRESHOLD:
157
+ return CHUNK_SIZE_DAYS_HIGH_GEO
158
+ return CHUNK_SIZE_DAYS_LOW_GEO
159
+
160
+ def _fetch_one(
161
+ self, start: str, end: str, base_params: dict[str, Any],
162
+ ) -> list[dict]:
163
+ """Fetch a single date-range chunk from the ESIOS API."""
164
+ params = {
165
+ **base_params,
166
+ "start_date": start,
167
+ "end_date": end + "T23:59:59",
168
+ }
169
+ logger.debug("Fetch %s → %s", start, end)
170
+ data = self._manager._get(f"indicators/{self.id}", params=params)
171
+ return data.get("indicator", {}).get("values", [])
172
+
173
+ def _fetch_chunks(
174
+ self,
175
+ gaps: list,
176
+ base_params: dict[str, Any],
177
+ max_workers: int = DEFAULT_CHUNK_WORKERS,
178
+ ) -> list[dict]:
179
+ """Fetch all gap chunks concurrently, return values in order.
180
+
181
+ Builds a list of (start, end) chunks from the gaps, then fetches
182
+ them in parallel using a thread pool. Results are reassembled in
183
+ chronological order.
184
+ """
185
+ chunk_delta = timedelta(days=self._chunk_days)
186
+
187
+ # Build chunk list
188
+ chunks: list[tuple[str, str]] = []
189
+ for gap in gaps:
190
+ current = gap.start
191
+ while current <= gap.end:
192
+ chunk_end = min(current + chunk_delta, gap.end)
193
+ chunks.append((
194
+ current.strftime("%Y-%m-%d"),
195
+ chunk_end.strftime("%Y-%m-%d"),
196
+ ))
197
+ current = chunk_end + timedelta(days=1)
198
+
199
+ if not chunks:
200
+ return []
201
+
202
+ if len(chunks) == 1:
203
+ return self._fetch_one(chunks[0][0], chunks[0][1], base_params)
204
+
205
+ # Fetch concurrently, preserve order
206
+ results: list[list[dict] | None] = [None] * len(chunks)
207
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
208
+ futures = {
209
+ pool.submit(self._fetch_one, s, e, base_params): i
210
+ for i, (s, e) in enumerate(chunks)
211
+ }
212
+ for future in as_completed(futures):
213
+ idx = futures[future]
214
+ results[idx] = future.result()
215
+
216
+ # Flatten in chronological order
217
+ all_values: list[dict] = []
218
+ for chunk_values in results:
219
+ if chunk_values:
220
+ all_values.extend(chunk_values)
221
+ return all_values
222
+
134
223
  def historical(
135
224
  self,
136
225
  start: str,
@@ -143,11 +232,15 @@ class IndicatorHandle:
143
232
  time_trunc: str | None = None,
144
233
  geo_trunc: str | None = None,
145
234
  column_name: str | None = None,
235
+ chunk_workers: int = DEFAULT_CHUNK_WORKERS,
146
236
  ) -> pd.DataFrame:
147
237
  """Fetch historical values as a DataFrame with DatetimeIndex.
148
238
 
149
239
  Uses local parquet cache when enabled. Only fetches missing date ranges
150
- from the API. Automatically chunks requests exceeding ~3 weeks.
240
+ from the API. Automatically chunks requests and fetches concurrently.
241
+
242
+ Chunk size adapts to the indicator's geo count: 180 days for low-geo
243
+ indicators, 21 days for high-geo (≥15 geos) to avoid ESIOS timeouts.
151
244
 
152
245
  When multiple geo_ids are present (e.g. indicator 600 returns data for
153
246
  several countries), the result is pivoted so each geo becomes a column
@@ -158,6 +251,8 @@ class IndicatorHandle:
158
251
  Useful for single-column results where a stable name like
159
252
  ``"value"`` is preferred over the default geo_name or
160
253
  indicator ID.
254
+ chunk_workers: Number of concurrent threads for fetching chunks.
255
+ Defaults to 4. Set to 1 for sequential fetching.
161
256
  """
162
257
  base_params: dict[str, Any] = {
163
258
  "locale": locale,
@@ -211,24 +306,8 @@ class IndicatorHandle:
211
306
  from esios.cache import DateRange
212
307
  gaps = [DateRange(start_date, end_date)]
213
308
 
214
- # -- Fetch missing ranges ----------------------------------------------
215
- all_values: list[dict] = []
216
- chunk_delta = timedelta(days=CHUNK_SIZE_DAYS)
217
-
218
- for gap in gaps:
219
- current = gap.start
220
- gap_end = gap.end
221
- while current <= gap_end:
222
- chunk_end = min(current + chunk_delta, gap_end)
223
- params = {
224
- **base_params,
225
- "start_date": current.strftime("%Y-%m-%d"),
226
- "end_date": chunk_end.strftime("%Y-%m-%d") + "T23:59:59",
227
- }
228
- logger.debug("Fetch %s → %s", params["start_date"], params["end_date"])
229
- data = self._manager._get(f"indicators/{self.id}", params=params)
230
- all_values.extend(data.get("indicator", {}).get("values", []))
231
- current = chunk_end + timedelta(days=1)
309
+ # -- Fetch missing ranges (concurrent + adaptive chunk size) -----------
310
+ all_values = self._fetch_chunks(gaps, base_params, max_workers=chunk_workers)
232
311
 
233
312
  # Learn any new geo mappings from the response
234
313
  self._enrich_geo_map(all_values)
esios/processing/i90.py CHANGED
@@ -33,6 +33,43 @@ def _any_value_greater_than_30(series: np.ndarray) -> bool:
33
33
  return any(v > 30 for v in series if isinstance(v, (int, float, np.integer, np.floating)) and not np.isnan(v))
34
34
 
35
35
 
36
+ # Labels that REE uses for the cells sitting between the index columns and the
37
+ # per-period value columns of an I90 sheet. Match is exact (case-insensitive,
38
+ # trimmed). The count of these cells per sheet has varied across REE format
39
+ # revisions — historically 2 ("Hora" / "Cuarto de Hora del dia" + "Total");
40
+ # from Oct 2025 the MTU 15-min transition dropped "Total" on several sheets,
41
+ # leaving 1. Counting them dynamically keeps the parser resilient to either.
42
+ _SEPARATOR_LABELS = frozenset({
43
+ "cuarto de hora del dia",
44
+ "hora del dia",
45
+ "hora",
46
+ "total",
47
+ "indicadores",
48
+ })
49
+
50
+
51
+ def _count_header_separators(row: np.ndarray, idx_col_start: int) -> int:
52
+ """Count separator cells immediately preceding the time-value block.
53
+
54
+ Walks ``row`` backwards from ``idx_col_start - 1``, incrementing on each
55
+ cell whose text matches a known separator label and stopping at the first
56
+ non-matching cell or NaN. A NaN cell signals the start of the index-column
57
+ placeholder zone in double-header layouts (where index labels live on the
58
+ other header row, leaving the date row blank under each index position).
59
+ """
60
+ n = 0
61
+ for i in range(idx_col_start - 1, -1, -1):
62
+ cell = row[i]
63
+ if cell is None or (isinstance(cell, float) and np.isnan(cell)):
64
+ break
65
+ text = str(cell).strip().lower()
66
+ if text in _SEPARATOR_LABELS:
67
+ n += 1
68
+ continue
69
+ break
70
+ return n
71
+
72
+
36
73
  class I90Book:
37
74
  """Represents an I90DIA workbook (XLS) with lazy sheet preprocessing.
38
75
 
@@ -221,6 +258,11 @@ class I90Sheet:
221
258
  return pd.DataFrame()
222
259
 
223
260
  columns_date = self._normalize_datetime_columns(columns_prior[idx_col_start:])
261
+ # _normalize_datetime_columns sets _n_columns_totals from the time-block
262
+ # content (NaN-filler vs sequential). Override with a header-label count
263
+ # so the index slice survives REE format revisions that add or drop a
264
+ # "Total" column without touching the time-axis encoding.
265
+ self._n_columns_totals = _count_header_separators(columns_prior, idx_col_start)
224
266
  columns_variable = columns[idx_col_start:]
225
267
  columns_index = columns[: idx_col_start - self._n_columns_totals]
226
268
 
@@ -230,6 +272,7 @@ class I90Sheet:
230
272
  self, idx_col_start: int, columns: np.ndarray
231
273
  ) -> tuple[np.ndarray, np.ndarray, np.ndarray, None]:
232
274
  columns_date = self._normalize_datetime_columns(columns[idx_col_start:])
275
+ self._n_columns_totals = _count_header_separators(columns, idx_col_start)
233
276
  columns_index = columns[: idx_col_start - self._n_columns_totals]
234
277
  return columns, columns_index, columns_date, None
235
278
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-esios
3
- Version: 2.3.0
3
+ Version: 2.4.1
4
4
  Summary: A Python wrapper for the ESIOS API (Spanish electricity market)
5
5
  Project-URL: Homepage, https://github.com/datons/python-esios
6
6
  Project-URL: Repository, https://github.com/datons/python-esios
@@ -85,7 +85,7 @@ from esios import ESIOSClient
85
85
  client = ESIOSClient()
86
86
 
87
87
  # Get indicator data as DataFrame
88
- handle = client.indicators.get(600) # PVPC price
88
+ handle = client.indicators.get(600) # Day-ahead spot price (OMIE)
89
89
  df = handle.historical("2025-01-01", "2025-01-31")
90
90
 
91
91
  # Search indicators
@@ -99,8 +99,8 @@ client.archives.download(1, start="2025-01-01", end="2025-01-31", output_dir="./
99
99
 
100
100
  | ID | Name | Description |
101
101
  |----|------|-------------|
102
- | 600 | PVPC | Voluntary price for small consumers |
103
- | 1001 | Day-ahead price | OMIE spot market price |
102
+ | 600 | Day-ahead price | OMIE spot market price |
103
+ | 1001 | PVPC | Voluntary price for small consumers (2.0TD) |
104
104
  | 10033 | Demand | Real-time electricity demand |
105
105
  | 10034 | Wind generation | Real-time wind generation |
106
106
  | 10035 | Solar PV generation | Real-time solar generation |
@@ -3,9 +3,9 @@ esios/async_client.py,sha256=OVNNZwFbvPyUnu7LVr7X5MdXlk_-AJ1lfkUE0OODlbQ,3452
3
3
  esios/cache.py,sha256=GgbrL9Rc9aLrEWHvXtQOCGQRgq2T4m6VBJDvBJfWMTk,18920
4
4
  esios/catalog.py,sha256=xWwMx5I32m34npjAXHh-Ua4e_0pfG89yxUC_Vy9VlAA,16811
5
5
  esios/client.py,sha256=rLgdyPFII6CC_TJwgkHaScJ7nBUpt85N94mujKAn0d0,5825
6
- esios/constants.py,sha256=pwB2UlBI96zYBA8wAbcCSHcm_E-aIj2hBarDA8t1Vp8,474
6
+ esios/constants.py,sha256=yfxSNG37i4dkpa7x0CBvXTroyddn5jhNTuWGDhAq3-0,1074
7
7
  esios/exceptions.py,sha256=AiWLdRDWj50JEsld9CvVBsfLnZZKFmW62_bZmZ7Z_eA,899
8
- esios/.agents/skills/esios/SKILL.md,sha256=_5wCzMMB8FHWcAPeMA5vGklZFEGBEvU5wBOryNIogzM,6252
8
+ esios/.agents/skills/esios/SKILL.md,sha256=D1wXiKyk7HoFw6CapccoORrtMXUpS2BuAVEChLu3AJE,6375
9
9
  esios/cli/__init__.py,sha256=9gd5ZDIH1-yNP_xcd60ethOFXm9w6un0CJ9CX0Qvb2A,256
10
10
  esios/cli/app.py,sha256=j1d8QWtKTTsWozSqqQitTkzzRjBE6OXY0ZZWYdS19wE,1524
11
11
  esios/cli/archives.py,sha256=Re9ZMauTiJlHdmiE7F3ZlV2wfaEyShS0C7Z4M2X4Ra8,7715
@@ -24,7 +24,7 @@ esios/data/time_periods.yaml,sha256=oyisKYYyOGA57eEAqkFFx6B3x9rdSl0DokZe5gNZfMw,
24
24
  esios/managers/__init__.py,sha256=-1AwL7arUf7WEZn1RSiK_DZhY3j6U4GE9_dqjbukCJc,268
25
25
  esios/managers/archives.py,sha256=PG-1gQYEiJUVQQtTKIZeEoWIsS-gkWT3ZHy89c8tTW8,9293
26
26
  esios/managers/base.py,sha256=7XcdrUtUOPuqfHYlz4w562TD8o9cNdBWOgs4CHHonoo,835
27
- esios/managers/indicators.py,sha256=nbmKsvBTPO2w3FlcVYv9WOtCTU8xMjFvT_d1AcA2sbg,17506
27
+ esios/managers/indicators.py,sha256=4f1wLhT33Fc93ixHr51DIzIBqzznJSaoeLfWOT-2EQ0,20260
28
28
  esios/managers/offer_indicators.py,sha256=0MjEKkj77YC2fRSHVTEc7FW6E8AuwwciAXK-bOVEL5Q,4187
29
29
  esios/models/__init__.py,sha256=oppuTASpf0Dh2KbGMXInULT0F4sELjeo-9UhPiPOZiA,289
30
30
  esios/models/archive.py,sha256=P2LaT7_ff4ujwqVn_ofgQP3dbpf7jqON0R22dKwSJ_w,1062
@@ -32,10 +32,10 @@ esios/models/indicator.py,sha256=u1AJyEA3YeOqQFjV08_lzyMaofuCiMoLPjvosls9gfE,111
32
32
  esios/models/offer_indicator.py,sha256=nA80Y7Yp0utDaDOdZ-ObcWTsAdhvuXlfJjJBpdVQ7Lo,758
33
33
  esios/processing/__init__.py,sha256=1kLt_gO_wDhXM1BbY0zTyfAYo-CjYKW1ljgRRDZ7USM,278
34
34
  esios/processing/dataframes.py,sha256=OitzBvAerssGP2VXNC-sSO48XsHdIB2nKTUgByN5eYQ,2524
35
- esios/processing/i90.py,sha256=fI8DfY8CD2kF1_ZrAzuEDxN0m7Vh3CV3dIn32lxKffA,11687
35
+ esios/processing/i90.py,sha256=UZEI6f0pG6mbMaVTh_L3KtUp9PuvxvfXYaG9-ePNIxg,13622
36
36
  esios/processing/zip.py,sha256=12LbFHJTdX_h3JG-clEgQ4Haj-kw0UjfopGLlCRXfGM,1913
37
- python_esios-2.3.0.dist-info/METADATA,sha256=KtWOIGA-o9z7mxZD8vryWTOntVZbxNm_LIVaXYhFD3g,3169
38
- python_esios-2.3.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
39
- python_esios-2.3.0.dist-info/entry_points.txt,sha256=7ngseyIyvJ4buTHFL9htaZ4tTFHpG4zzJNkc8B5Jr8U,40
40
- python_esios-2.3.0.dist-info/licenses/LICENSE,sha256=LorLs1-VeBW70Wo9fLAtLJN7nNd6Poy0xzvqdWVqFlE,35128
41
- python_esios-2.3.0.dist-info/RECORD,,
37
+ python_esios-2.4.1.dist-info/METADATA,sha256=_qFX68pzna9JIBW1zm57X8WNxZ70BO8zaxC4g1nbmTM,3194
38
+ python_esios-2.4.1.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
39
+ python_esios-2.4.1.dist-info/entry_points.txt,sha256=7ngseyIyvJ4buTHFL9htaZ4tTFHpG4zzJNkc8B5Jr8U,40
40
+ python_esios-2.4.1.dist-info/licenses/LICENSE,sha256=LorLs1-VeBW70Wo9fLAtLJN7nNd6Poy0xzvqdWVqFlE,35128
41
+ python_esios-2.4.1.dist-info/RECORD,,