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. 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, >>> 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. 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>`.