Overview
Theesios.processing.dataframes module provides utilities for transforming ESIOS API responses into clean, timezone-aware pandas DataFrames. These functions handle datetime parsing, timezone conversion, and automatic pivoting for multi-geography datasets.
to_dataframe
Convert a list of ESIOS value dictionaries to a pandas DataFrame.Parameters
List of dictionaries containing ESIOS indicator values. Each dict typically includes fields like
datetime, value, geo_id, and geo_name.Target timezone for the datetime index. Defaults to
TIMEZONE constant ("Europe/Madrid").When
True and multiple geo_id values are present, pivots the DataFrame so each geography becomes a separate column (wide format). When False, keeps the raw format with geo_id and geo_name columns intact.Returns
pd.DataFrame — A pandas DataFrame with:
- A timezone-aware
DatetimeIndex - Cleaned columns (time-related auxiliary columns removed)
- For single-geo data: a flat DataFrame with
valuecolumn - For multi-geo data with
pivot_geo=True: each geography as a column - For multi-geo data with
pivot_geo=False: long format withgeo_id,geo_name, andvaluecolumns
Behavior
- Datetime Parsing: Parses the
datetimefield to aDatetimeIndexand converts from UTC to the target timezone - Column Cleanup: Drops auxiliary time columns (any column containing “time” except “datetime”)
- Geography Handling:
- Single geography: Drops
geo_idandgeo_namecolumns - Multiple geographies (with
pivot_geo=True): Pivots so each geography becomes a column, usinggeo_nameas column labels (falls back togeo_idif names aren’t available) - Multiple geographies (with
pivot_geo=False): Keeps raw format intact
- Single geography: Drops
Examples
Basic Usage (Single Geography)
Multi-Geography (Automatic Pivot)
Multi-Geography (Disable Pivot)
Custom Timezone
convert_timezone
Convert a DataFrame’s DatetimeIndex to another timezone.Parameters
DataFrame with a DatetimeIndex to convert.
Target timezone string (e.g.,
"UTC", "America/New_York", "Europe/Madrid").Returns
pd.DataFrame — The same DataFrame with the index converted to the target timezone. If the index is timezone-naive, it will first be localized to UTC before conversion.
Examples
Convert from Madrid Time to UTC
Handle Naive DatetimeIndex
If the DataFrame has a naive (timezone-unaware) DatetimeIndex, it will be automatically localized to UTC first:Integration with Client
These functions are used internally by the ESIOS client when fetching indicator data:Notes
- The default timezone
"Europe/Madrid"matches the timezone used by ESIOS for all timestamps - Time columns like
tz_time,datetime_utc, etc. are automatically removed to reduce clutter - Empty input (
data=[]) returns an empty DataFrame - The pivoting logic uses
pivot_tablewithaggfunc="first"to handle potential duplicates
