Instances ========= This page shows how to load instances of time series. All examples below expects you to have an initialized instance of the client called ``eq``. Operations described here are available under ``eq.instances.*``. **Requirements:** Use these operations for curves with ``curve_type`` set to any of the following: * ``INSTANCE`` Load instances -------------- Method reference: :py:meth:`eq.instances.load() ` To load multiple instances (typically forecasts), you only need to specify the **curve**. By default, it will load 5 instances, but you can increase (or decrease) this by specifying a ``limit`` option. You can also filter by **issue date** and **tags**. Let's start by loading the latest instances for the German wind power forecasts: >>> forecasts = eq.instances.load( >>> 'DE Wind Power Production MWh/h 15min Forecast' >>> ) >>> forecasts [, curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 14:00:00+02:00", end="2020-07-11 14:00:00+02:00">, , curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 14:00:00+02:00", end="2020-07-05 14:00:00+02:00">, , curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 14:00:00+02:00", end="2020-07-10 14:00:00+02:00">, , curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 14:00:00+02:00", end="2020-07-05 14:00:00+02:00">, , curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 08:00:00+02:00", end="2020-07-11 08:00:00+02:00">] The return type from ``load()`` is a :py:class:`~energyquantified.data.TimeseriesList`. This is a subclass of Python's built-in list. It has extra validations so that all time series in the list have the same frequency (hourly, daily, etc.), and it has a method called :py:meth:`~energyquantified.data.TimeseriesList.to_dataframe` which converts the list of time series to a ``pandas.DataFrame``. See the chapter on :doc:`Pandas integration ` for more details. Notice that each time series in the list has an ``instance`` attribute (with ``issued`` (issue date) and ``tag``). This is what identifies the instances (or forecasts, if you will): >>> [f.instance for f in forecasts] [, , , , ] Energy Quantified has many years of forecasts in the database (non-paying users only get access to instances from the latest 30 days). So, if you would like to load older instances, you can do so! Here is how to load the last instances issued in 2018: >>> from datetime import datetime >>> forecasts = eq.instances.load( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> issued_at_latest=datetime(2018, 12, 31, 23, 59, 59) # Last second of 2018! >>> ) You can also filter by the instance's **tags** you would like to load. It is possible to list more than one tag. Let's download data for ``ec`` and ``gfs``: >>> forecasts = eq.instances.load( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> tags=['ec', 'gfs'] # Can be a string, too: tags='ec' >>> ) Combine the parameters ``issued_at_latest`` and ``tags`` to load the instances of your liking. There is also an ``exclude_tags`` to let you remove certain tags from the response. Use the ``time_zone`` parameter to convert the data to the given timezone: >>> from energyquantified.time import UTC >>> >>> forecasts = eq.instances.load( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> time_zone=UTC >>> ) And finally, you can aggregate instances: >>> from datetime import datetime >>> from energyquantified.time import Frequency >>> from energyquantified.metadata import Aggregation, Filter >>> forecasts = eq.instances.load( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> issued_at_latest=datetime(2020, 6, 1, 0, 0, 0), >>> tags='ec', >>> frequency=Frequency.P1D, >>> aggregation=Aggregation.AVERAGE, >>> hour_filter=Filter.BASE, >>> threshold=7 # Aggregate days only if 7 or fewer quarters are missing >>> limit=10 >>> ) Get the latest instance ----------------------- Method reference: :py:meth:`eq.instances.latest() ` You can load the latest instance available like so: >>> forecast = eq.instances.latest( >>> 'DE Wind Power Production MWh/h 15min Forecast' >>> ) >>> forecast , curve="DE Wind Power Production MWh/h 15min Forecast", instance=, begin="2020-06-25 20:00:00+02:00", end="2020-06-26 10:00:00+02:00"> As for the method to load multiple instances, you can put filters on which instance you would like to load: >>> from datetime import datetime >>> forecast = eq.instances.latest( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> tags='ec', >>> issued_at_latest=datetime(2020, 6, 1, 0, 0, 0) >>> ) Aggregations are supported here, too. Get a specific instance ----------------------- Method reference: :py:meth:`eq.instances.get() ` If you know the **issue date** and **tag** for an instance, you can load it like seen below. You must always specify the issue date, but you can leave the tag unspecified (which will default to a blank tag). >>> from datetime import datetime >>> forecast = eq.instances.get( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> issued=datetime(2020, 6, 1, 0, 0, 0), >>> tag='ec' >>> ) >>> forecast.instance Aggregations are supported here, too. Include ensembles ----------------- All the above methods — ``load()``, ``latest()`` and ``get()`` — can also load *scenarios* for instances that have these. For instance-based data, we refer to *scenarios* as *ensembles*. The terminology comes from meteorology, where forecasts with multiple scenarios are called *ensemble forecasts*. To load ensembles, add ``ensembles=True`` in the parameters. There is one catch: When loading ensembles, the maximum number of instances you can load at once becomes reduced to 10 due to increased server-side load. Instances that don't have ensembles will return a regular, single-valued time series. In the below example, we are loading the GFS ensemble forecast issued 1 June 2020 at 00:00. And aggregations are supported here, too: >>> from datetime import datetime >>> forecast = eq.instances.get( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> issued=datetime(2020, 6, 1, 0, 0, 0), >>> tag='gfs-ens', # GFS ensemble forecast >>> frequency=Frequency.P1D, >>> ensembles=True # Include ensembles >>> ) >>> forecast.data[:3] [, , ] Relative queries (day-ahead forecasts) -------------------------------------- Method reference: :py:meth:`eq.instances.relative() ` When benchmarking models (forecasts), one often would like to know what a forecast was for the day ahead. And you would like to do this over a date interval. For example, we would like to know Monday's forecast for Tuesday, and Tuesday's forecast for Wednesday, and so on. Energy Quantified's API has solved this by via an operation we call *relative forecasts*. The relative forecasts work for **0 or more days ahead**: - ``days_ahead=0`` means forecasts for intraday - ``days_ahead=1`` means forecasts for day ahead - ``days_ahead=2`` means forecasts for day after day ahead - `... and so on` You *must* filter on the **tag**, and you *can* filter on the **time-of-day** the forecast was issued. When there isn't any forecast issued for a specific day, then that day will have no values. >>> from datetime import datetime, time >>> day_ahead_forecast = eq.instances.relative( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> begin=datetime(2020, 6, 1, 0, 0, 0), >>> end=datetime(2020, 6, 5, 0, 0, 0), >>> tag='ec', >>> days_ahead=1, # The day-ahead forecast (0 or higher allowed) >>> time_of_day=time(0, 0), # Issued at exactly 00:00 >>> frequency=Frequency.P1D >>> ) >>> day_ahead_forecast.data [, , , ] If you don't know precisely when the forecast was issued, or you would like only to get forecasts issued before a particular time of the day, use the **before_time_of_day** instead. You can also decide whether to select the *earliest* or *latest* issued instance by specifying the **issued** parameter. There is also a parameter for **after_time_of_day**. Here we select the *latest day ahead* wind power forecasts issued *before 12:00* every day from 1 June to 5 June: >>> from datetime import datetime, time >>> day_ahead_forecast = eq.instances.relative( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> begin=datetime(2020, 6, 1, 0, 0, 0), >>> end=datetime(2020, 6, 5, 0, 0, 0), >>> tag='ec', >>> days_ahead=1, >>> before_time_of_day=time(12, 0), # Issued before 12 o'clock >>> issued='latest', # Set to "earliest" or "latest" >>> frequency=Frequency.P1D >>> ) >>> day_ahead_forecast.data [, , , ] Aggregations are also supported, as you can see from the examples above. Rolling forecasts (hours-ahead forecasts) -------------------------------------- Method reference: :py:meth:`eq.instances.rolling() ` Rolling forecasts are time series where each value is from the latest instance (forecast) created at least ``hours_ahead`` hours before. If the time series from a rolling forecast with ``hours_ahead=2`` contains the dates ``2023-10-01 05:15:00`` and ``2023-10-01 05:30:00``, then the values are from the latest instance (forecast) created at or before ``2023-10-01 03:15:00`` and ``2023-10-01 03:30:00``, respectively. The example below illustrates how to load a rolling forecast for wind power in Germany from ``2023-10-01`` (inclusive) to ``2023-10-02`` (exclusive): >>> from datetime import datetime >>> rolling_forecast = eq.instances.rolling( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> begin=datetime(2023, 10, 1), >>> end=datetime(2023, 10, 2), >>> hours_ahead=2, >>> ) >>> rolling_forecast.data [, , , ..., , , ] Specify the ``tags`` parameter to create a rolling forecast with values from instances with a matching tag only. Similarly, filter out specific instances with the ``exclude_tags`` parameter. The following code snippet shows how to load the rolling forecast for ``DE Wind Power Production MWh/h 15min Forecast`` from ``2023-10-01`` (inclusive) to ``2023-10-02``, using values from instances with the ``ec`` or ``gfs`` tag. The result is filtered for ``PEAK``, aggregated from quarter-hourly to hourly frequency, and unit converted from ``MWh/h`` to ``GWh/h``. >>> from datetime import datetime >>> from energyquantified.metadata import Filter >>> rolling_forecast = eq.instances.rolling( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> begin=datetime(2023, 10, 1), >>> end=datetime(2023, 10, 2), >>> hours_ahead=2, >>> tags=['ec', 'gfs'], >>> frequency=Frequency.PT1H, >>> hour_filter=Filter.PEAK, >>> unit="GWh/h", >>> ) >>> rolling_forecast.data [ ] Please see the full method reference for additional load options and further information. Absolute forecasts queries -------------------------- Method reference: :py:meth:`eq.instances.absolute() ` Load forecasted values from various instances for a specific point in time, to see how forecasts develop over time. The following parameters are required to load absolute forecasts: * ``curve``: The :py:class:`Curve ` to load * ``delivery``: The point in time to load forecasted values for * ``begin``: Earliest issued date for instances (inclusive) * ``end``: Latest issued date for instances (exclusive) The example below loads the absolute forecast for ``DE Wind Power Production MWh/h 15min Forecast`` at 2023-10-05 12:00, from instances issued between 2023-10-01 (inclusive) and 2023-10-02 (exclusive). >>> from datetime import datetime >>> absolute_result = eq.instances.absolute( >>> "DE Wind Power Production MWh/h 15min Forecast", >>> datetime(2023, 10, 5, 12), # Delivery 2023-10-05 12:00 >>> begin=datetime(2023, 10, 1), # Earliest issued (inclusive) >>> end=datetime(2023, 10, 2), # Latest issued (exclusive) >>> ) >>> print(absolute_result) , delivery=2023-10-05 12:00:00+02:00, filters=BASE, aggregation=MEAN, unit=MWh/h, items= , value=33493.8>, , value=35230.1>, , value=24395>, ..., , value=24648.8>, , value=29499.2>, , value=26542.6>> The resolution of the delivery defaults to the Curve's resolution. Change the frequency or time zone of the delivery by supplying the ``frequency`` or ``time_zone`` parameters, respectively. Note that you cannot use a frequency higher than the Curve's original frequency, and time zone conversion is supported only for curves with higher than daily frequency (> P1D). Load the absolute forecast with delivery in hourly frequency from instances with the ``ec`` tag: >>> from datetime import datetime >>> from energyquantified.time import Frequency >>> absolute_result = eq.instances.absolute( >>> "DE Wind Power Production MWh/h 15min Forecast", >>> datetime(2023, 10, 5, 12), # Delivery 2023-10-05 12:00 >>> begin=datetime(2023, 10, 1), # Earliest issued (inclusive) >>> end=datetime(2023, 10, 2), # Latest issued (exclusive) >>> frequency=Frequency.PT1H, >>> tags=["ec"] >>> ) >>> print(absolute_result) , delivery=2023-10-05 12:00:00+02:00, filters=BASE, aggregation=MEAN, unit=MWh/h, items= , value=23897.75>, , value=31124.48>> Set zone of the delivery with the ``time_zone`` parameter: >>> from datetime import datetime >>> from energyquantified.time import Frequency, UTC >>> absolute_result = eq.instances.absolute( >>> "DE Wind Power Production MWh/h 15min Forecast", >>> datetime(2023, 10, 5, 12), # Delivery 2023-10-05 12:00 >>> begin=datetime(2023, 10, 1), # Earliest issued (inclusive) >>> end=datetime(2023, 10, 2), # Latest issued (exclusive) >>> frequency=Frequency.PT1H, >>> time_zone=UTC, >>> tags=["ec"] >>> ) >>> print(absolute_result) , delivery=2023-10-05 12:00:00+00:00, filters=BASE, aggregation=MEAN, unit=MWh/h, items= , value=20479.78>, , value=29158.65>> The :py:meth:`absolute() ` method also supports filtering on specific hours (e.g., BASE/PEAK), unit conversion, choosinghow to aggregate data when delivery frequency is lower than the Curve's, and options to filter or exclude instances by tags. The final example below illustrates and explains usage of all parameters simultaneously: >>> from datetime import datetime >>> from energyquantified.metadata import Aggregation, Filter >>> from energyquantified.time import Frequency, UTC >>> absolute_result = eq.instances.absolute( >>> "DE Wind Power Production MWh/h 15min Forecast", >>> # Delivery 2023-10-05 >>> datetime(2023, 10, 5), >>> # Earliest issued (inclusive) for instances to use >>> begin=datetime(2023, 10, 1), >>> # Latest issued (exclusive) for instances to use >>> end=datetime(2023, 10, 2), >>> # Frequency of the delivery >>> frequency=Frequency.P1D, >>> # Timezone of the delivery >>> time_zone=UTC, >>> # Exclude values not in PEAK when aggregating >>> hour_filter=Filter.PEAK, >>> # Convert the unit from 'MWh/h' to 'GWh/h' >>> unit="GWh/h, >>> # We load with a lower frequency (P1D < Curve's original >>> # PT15M), so all of the PT15M values must be aggregated >>> # into a single value representing the delivery. Aggregate >>> # to AVERAGE (or MEAN). >>> aggregation=Aggregation.AVERAGE, >>> tags=["ec"], # Only include instances with tag 'ec' >>> exclude_tags=["gfs"], # Exclude instance with tag 'gfs' >>> ) >>> print(absolute_result) , delivery=2023-10-05 00:00:00+00:00, filters=PEAK, aggregation=AVERAGE, unit=GWh/h, items= , value=16.81>, , value=23.1>> List available instances and tags --------------------------------- There are two utility methods available under ``eq.instances.*``: Tags ^^^^ Method reference: :py:meth:`eq.instances.tags() ` List the unique tags that exist in instances for a curve. The response is a Python set of the existing tags: >>> eq.instances.tags( >>> 'DE Wind Power Production MWh/h 15min Forecast' >>> ) {'ec', 'ec-ens', 'ecsr', 'ecsr-ens', 'gfs', 'gfs-ens'} List instances ^^^^^^^^^^^^^^ Method reference: :py:meth:`eq.instances.list() ` Similar to the ``load()``-method, but this method only lists the *instances* instead of loading the time series data: >>> eq.instances.list( >>> 'DE Wind Power Production MWh/h 15min Forecast', >>> issued_at_latest='2020-05-01 00:00', >>> tags='gfs', >>> limit=10 >>> ) [, , , , , , , , , ] ----- Next steps ---------- Learn how to load :doc:`time series <../userguide/timeseries>`, :doc:`period-based series <../userguide/periods>`, and :doc:`period-based series instances <../userguide/period-instances>`.