Skip to content

Commit

Permalink
Portfolyo0.4 (#4)
Browse files Browse the repository at this point in the history
* updated to use portfolyo 0.4

* better docs

* matching portfolyo version

Co-authored-by: Ruud Wijtvliet <[email protected]>
  • Loading branch information
rwijtvliet and rwijtvliet-lb authored Nov 30, 2022
1 parent 144be19 commit c71ad97
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
5 changes: 2 additions & 3 deletions belvys/adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pandas as pd
import portfolyo as pf
from portfolyo.tools import frames

Adjustment = Callable[[pd.Series, int, str, str], pd.Series]
Adjustment = Callable[[pd.Series], pd.Series]
Expand Down Expand Up @@ -105,7 +104,7 @@ def fact_frequency(freq: str) -> Adjustment:
can be inferred."""

def adjustment(s: pd.Series) -> pd.Series:
return frames.set_frequency(s, freq)
return pf.tools.freq.set_to_frame(s, freq)

return adjustment

Expand All @@ -121,7 +120,7 @@ def makeleft(s: pd.Series) -> pd.Series:
first two timestamps."""
td = s.index[1] - s.index[0]
if td <= dt.timedelta(hours=2):
s.index = pf.right_to_left(s.index)
s.index = pf.tools.righttoleft.index(s.index)
return s


Expand Down
8 changes: 4 additions & 4 deletions belvys/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from . import adjustment
from .adjustment import Adjustment
from .api import Api
from .structure import Structure, Ts, TsTree
from .common import print_status
from .structure import Structure, Ts, TsTree


def fact_default_aftercare(tz) -> Adjustment:
Expand Down Expand Up @@ -183,7 +183,7 @@ def general_pfl(
pf.PfLine
"""
# Fix timestamps.
ts_left, ts_right = pf.ts_leftright(ts_left, ts_right)
ts_left, ts_right = pf.tools.leftandright.stamps(ts_left, ts_right)
# Get ts tree and fetch data.
ts_tree = Ts(pfid, tsname)
self.api.series(ts_tree, ts_left, ts_right, missing2zero=missing2zero)
Expand Down Expand Up @@ -227,7 +227,7 @@ def portfolio_pfl(
pf.PfLine
"""
# Fix timestamps.
ts_left, ts_right = pf.ts_leftright(ts_left, ts_right)
ts_left, ts_right = pf.tools.leftandright.stamps(ts_left, ts_right)
# Get ts trees and fetch data.
ts_trees = []
for pfid in self.structure.to_original_pfids(pfid):
Expand Down Expand Up @@ -270,7 +270,7 @@ def price_pfl(
pf.PfLine
"""
# Fix timestamps.
ts_left, ts_right = pf.ts_leftright(ts_left, ts_right)
ts_left, ts_right = pf.tools.leftandright.stamps(ts_left, ts_right)
# Get ts trees and fetch data.
ts_tree = self.structure.tstree_price(priceid)
self._fetch_series(ts_tree, ts_left, ts_right, missing2zero=missing2zero)
Expand Down
10 changes: 6 additions & 4 deletions docs/aftercare.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Motivation

The ``Api.series()`` method returns a ``pandas`` Series (`documentation <https://pandas.pydata.org/docs/reference/api/pandas.Series.html>`_), which is close to how the data is returned by the Belvis Rest Api. In particular, the timestamps in the index are not altered in any way - they are simply put in a pandas ``DatetimeIndex``, without any timezone conversions or other changes.

The ``Tenant.portfolio_pfl()`` and ``Tenant.price_pfl()`` methods, however, return a ``portfolyo`` PfLine (`documentation <https://portfolyo.readthedocs.io/en/latest/core/pfline.html>`_), and the series above are not always in the correct format to use as input for this class. With the help of the aftercare function, the series can be changed before being used as input.
The ``Tenant.portfolio_pfl()`` and ``Tenant.price_pfl()`` methods, however, return a ``portfolyo`` PfLine (`documentation <https://portfolyo.readthedocs.io/en/latest/core/pfline.html>`_), and the series above are not always in the correct format to use as input for this class.

With the help of the aftercare function, the series can be changed before being used as input into ``PfLine``.

---------------------
Necessary adjustments
Expand Down Expand Up @@ -45,10 +47,10 @@ Frequency

.. code-block:: python
from portfolyo.tools import frames
import portfolyo as pf
def infer_frequency(s: pandas.Series) -> pandas.Series:
return frames.set_frequency(s)
return pf.tools.freq.set_to_frame(s)
Right-bound
-----------
Expand All @@ -60,7 +62,7 @@ Right-bound
def makeleft(s: pandas.Series) -> pandas.Series:
td = s.index[1] - s.index[0]
if td <= dt.timedelta(hours=2):
s.index = portfolyo.right_to_left(s.index)
s.index = pf.tools.righttoleft.index(s.index)
return s
Custom issues
Expand Down
84 changes: 42 additions & 42 deletions docs/tenant.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A tenant can be initialised using a ``Structure`` and an ``Api`` instance. Conti
Usage
-----

The most important methods are ``.portfolio_pfl()``, ``.price_pfl()``, and ``general_pfl()``. All return a ``portfolyo.PfLine`` (see `here <portfolyo.readthedocs.io>`_) instance. Here we see how they are used to get data for the 3-day (left-closed) time period from midnight 2022-09-05 until midnight 2022-09-08.
The most important methods are ``.portfolio_pfl()``, ``.price_pfl()``, and ``general_pfl()``. All return a ``portfolyo.PfLine`` (see `here <portfolyo.readthedocs.io>`_) instance. Here we see how they are used to get data for the 3-day (left-closed) time period from midnight 2024-09-05 until midnight 2024-09-08.

Portfolio data
--------------
Expand All @@ -40,27 +40,27 @@ Portfolio data
.. code-block:: python
# Continuation of previous example.
offtake = tenant.portfolio_pfl("B2C_household", "current_offtake", "2022-09-05", "2022-09-08")
offtake = tenant.portfolio_pfl("B2C_household", "current_offtake", "2024-09-05", "2024-09-08")
offtake
.. code-block:: text
PfLine object with volume information.
. Timestamps: first: 2022-09-05 00:00:00+02:00 timezone: Europe/Berlin
last: 2022-09-07 23:45:00+02:00 freq: <15 * Minutes> (288 datapoints)
w q
MW MWh
2022-09-05 00:00:00 +0200 -99.4 -25
2022-09-05 00:15:00 +0200 -98.4 -25
2022-09-05 00:30:00 +0200 -97.3 -24
2022-09-05 00:45:00 +0200 -95.9 -24
2022-09-05 01:00:00 +0200 -95.3 -24
. Start: 2024-09-05 00:00:00+02:00 (incl) . Timezone : Europe/Berlin
. End : 2024-09-08 00:00:00+02:00 (excl) . Start-of-day: 00:00:00
. Freq : <15 * Minutes> (288 datapoints)
w q
MW MWh
2024-09-05 00:00:00 +0200 -67.8 -17
2024-09-05 00:15:00 +0200 -61.3 -15
2024-09-05 00:30:00 +0200 -55.5 -14
2024-09-05 00:45:00 +0200 -50.9 -13
.. .. ..
2022-09-07 23:00:00 +0200 -112.4 -28
2022-09-07 23:15:00 +0200 -109.8 -27
2022-09-07 23:30:00 +0200 -107.3 -27
2022-09-07 23:45:00 +0200 -105.7 -26
2024-09-07 23:00:00 +0200 -105.7 -26
2024-09-07 23:15:00 +0200 -98.9 -25
2024-09-07 23:30:00 +0200 -91.4 -23
2024-09-07 23:45:00 +0200 -84.0 -21
Prices
------
Expand All @@ -70,27 +70,27 @@ Prices
.. code-block:: python
# Continuation of previous example.
prices = tenant.price_pfl("fwc_monthly_DE", "2022-09-05", "2022-09-08")
prices = tenant.price_pfl("fwc_monthly_DE", "2024-09-05", "2024-09-08")
prices
.. code-block:: text
PfLine object with price information.
. Timestamps: first: 2022-09-05 00:00:00+02:00 timezone: Europe/Berlin
last: 2022-09-07 23:45:00+02:00 freq: <15 * Minutes> (288 datapoints)
. Start: 2024-09-05 00:00:00+02:00 (incl) . Timezone : Europe/Berlin
. End : 2024-09-08 00:00:00+02:00 (excl) . Start-of-day: 00:00:00
. Freq : <15 * Minutes> (288 datapoints)
p
Eur/MWh
2022-09-05 00:00:00 +0200 274.89
2022-09-05 00:15:00 +0200 250.00
2022-09-05 00:30:00 +0200 257.86
2022-09-05 00:45:00 +0200 215.83
2022-09-05 01:00:00 +0200 249.90
2024-09-05 00:00:00 +0200 209.31
2024-09-05 00:15:00 +0200 189.06
2024-09-05 00:30:00 +0200 169.01
2024-09-05 00:45:00 +0200 147.89
.. ..
2022-09-07 23:00:00 +0200 499.12
2022-09-07 23:15:00 +0200 433.34
2022-09-07 23:30:00 +0200 377.55
2022-09-07 23:45:00 +0200 295.72
2024-09-07 23:00:00 +0200 198.68
2024-09-07 23:15:00 +0200 175.70
2024-09-07 23:30:00 +0200 152.81
2024-09-07 23:45:00 +0200 123.77
General
Expand All @@ -101,34 +101,34 @@ For convenience, the method ``.general_pfl()`` exists. It can be used to fetch d
.. code-block:: python
# Continuation of previous example.
churn = tenant.general_pfl("B2C_Household", "Expected churn in MW", "2022-09-05", "2022-09-08")
churn = tenant.general_pfl("B2C_Household", "Expected churn in MW", "2024-09-05", "2024-09-08")
churn
.. code-block:: text
PfLine object with volume information.
. Timestamps: first: 2022-09-05 00:00:00+02:00 timezone: Europe/Berlin
last: 2022-09-07 23:45:00+02:00 freq: <15 * Minutes> (288 datapoints)
. Start: 2024-09-05 00:00:00+02:00 (incl) . Timezone : Europe/Berlin
. End : 2024-09-08 00:00:00+02:00 (excl) . Start-of-day: 00:00:00
. Freq : <15 * Minutes> (288 datapoints)
w q
MW MWh
2022-09-05 00:00:00 +0200 -9.9 -2.5
2022-09-05 00:15:00 +0200 -9.8 -2.5
2022-09-05 00:30:00 +0200 -9.7 -2.4
2022-09-05 00:45:00 +0200 -9.5 -2.4
2022-09-05 01:00:00 +0200 -9.5 -2.4
2024-09-05 00:00:00 +0200 -6.8 -2
2024-09-05 00:15:00 +0200 -6.1 -2
2024-09-05 00:30:00 +0200 -5.6 -1
2024-09-05 00:45:00 +0200 -5.1 -1
.. .. ..
2022-09-07 23:00:00 +0200 -11.2 -2.8
2022-09-07 23:15:00 +0200 -10.9 -2.7
2022-09-07 23:30:00 +0200 -10.7 -2.7
2022-09-07 23:45:00 +0200 -10.5 -2.6
2024-09-07 23:00:00 +0200 -10.6 -3
2024-09-07 23:15:00 +0200 -9.9 -2
2024-09-07 23:30:00 +0200 -9.1 -2
2024-09-07 23:45:00 +0200 -8.4 -2
-----
Cache
-----

As mentioned in the :doc:`api` documentation, it can be time-consuming querying the api for timeseries IDs. Even if these are cached, it might be a good idea to explicitly fill the cache beforehand. This can be done using the ``.update_cache()`` menthod. It finds all timeseries in each of the relevant portfolios, and stores their ID in the cache. Any subsequent queries will therefore only use the API to find the *values* of the timeseries - which cannot be cached, as they constantly change.
As mentioned in the :doc:`api` documentation, it can be time-consuming querying the api for timeseries IDs. Even if these are cached, it might be a good idea to explicitly fill the cache beforehand. This can be done using the ``.update_cache()`` menthod. It finds all timeseries in each of the relevant portfolios, and stores their ID in the cache. Any subsequent queries will therefore only use the API to find the *values* of the timeseries - which cannot be cached (because there is many data, which also constantly changes).

Here too: if a file is specified when initialising the ``Api`` instance, or if the instance is initialised from a file (``Api.from_file()``), the cache is stored there and can be reused when the program ends.

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pandas
numpy
requests
tqdm
portfolyo==0.3.6
portfolyo==0.4.1
python-forge
versioneer

0 comments on commit c71ad97

Please sign in to comment.