Skip to content

Commit

Permalink
new tests with fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
rwijtvliet committed Nov 20, 2024
1 parent a32a493 commit 380fe1f
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 149 deletions.
50 changes: 30 additions & 20 deletions docs/core/pfline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ The properties ``PfLine.w``, ``.q``, ``.p`` and ``.r`` always return the informa
# --- hide: stop ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])

pfl.r
# --- hide: start ---
Expand All @@ -199,8 +200,9 @@ If we want to extract more than one timeseries, we can use the ``.df`` attribute
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.df
Expand All @@ -222,8 +224,9 @@ The ``PfLine.index`` property returns the ``pandas.DatetimeIndex`` that applies
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.index
Expand All @@ -237,8 +240,9 @@ For convenience, ``portfolyo`` adds a ``.duration`` and a ``right`` property to
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.index.duration, pfl.index.right
Expand All @@ -259,8 +263,9 @@ Another slicing method is implemented with the ``.slice[]`` property. The improv
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.slice['2024':'2026'] # excludes 2026; 2026 interpreted as timestamp 2026-01-01 00:00:00
Expand All @@ -278,8 +283,9 @@ A portfolio line can be reindexed with ``.index()``, using another index, e.g. o
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index)
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
index2 = pd.date_range('2025', freq='YS', periods=3)
Expand All @@ -299,8 +305,9 @@ Portfolio lines can be concatenated with the ``portfolio.concat()`` function. Th
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
index2 = pd.date_range('2025', freq='YS', periods=3) # 2 years' overlap with pfl
Expand Down Expand Up @@ -335,8 +342,9 @@ The data can be shown graphically with the ``.plot()`` method:
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.plot()
Expand All @@ -358,8 +366,9 @@ Alternatively, the data can be saved as an Excel workbook with the ``.to_excel()
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.to_clipboard()
Expand All @@ -378,8 +387,9 @@ Using the ``.asfreq()`` method, we can quickly and correctly downsample our data
# --- hide: start ---
import portfolyo as pf, pandas as pd
index = pd.date_range('2024', freq='YS', periods=3)
input_df = pd.DataFrame({'w':[200, 220, 300], 'p': [100, 150, 200]}, index).astype({'w': 'pint[MW]', 'p': 'pint[Eur/MWh]'})
pfl = pf.PfLine(input_df)
volume = pd.Series([200, 220, 300], index).astype('pint[MW]')
price = pd.Series([100, 150, 200], index).astype('pint[Eur/MWh]')
pfl = pf.PfLine([volume, price])
# --- hide: stop ---
# continuation of previous code example
pfl.asfreq('QS')
Expand Down
Binary file modified docs/savefig/fig_hedge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/savefig/fig_offtake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/savefig/fig_plot_pfl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion portfolyo/core/pfline/interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _multiple_union(inops: Iterable[InOp]) -> InOp:
return inop_result


def _union(inop1: InOp, inop2: InOp) -> InOp:
def _union(inop1: InOp, inop2: InOp | None) -> InOp:
"""Combine 2 ``InOp`` objects, and raise error if same dimension is supplied twice."""
if inop2 is None:
return inop1
Expand Down
27 changes: 27 additions & 0 deletions tests/tools/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest


@pytest.fixture(params=[None, "Europe/Berlin", "Asia/Kolkata"])
def tz(request) -> str | None:
return request.param


@pytest.fixture(params=["00:00", "06:00"])
def startofday(request) -> str:
return request.param


@pytest.fixture(params=["15min", "h", "D", "MS", "QS", "YS"])
def freq(request) -> str:
return request.param


@pytest.fixture(params=["30sec", "5min", "MS-FEB", "QS-FEB", "YS-FEB"])
def uncommon_freq(request) -> str:
"""Uncommon, but allowed, freq."""
return request.param


@pytest.fixture(params=["45min", "2h"])
def disallowed_freq(request) -> str:
return request.param
Loading

0 comments on commit 380fe1f

Please sign in to comment.