Skip to content

Commit

Permalink
minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
rwijtvliet committed May 23, 2024
1 parent 152016e commit aa972de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 42 deletions.
5 changes: 2 additions & 3 deletions portfolyo/tools/intersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ def frames(
Returns
-------
list of series and/or dataframes
As input, but trimmed to their intersection.
As input, but trimmed to their intersection.
Notes
-----
Expand All @@ -228,4 +227,4 @@ def frames(
ignore_tz=ignore_tz,
ignore_start_of_day=ignore_start_of_day,
)
return [fr.loc[idx] for idx, fr in zip(new_idxs, frames)]
return [fr.loc[i] for i, fr in zip(new_idxs, frames)]
33 changes: 6 additions & 27 deletions portfolyo/tools/product.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utilities for calculating / manipulating price data."""

import datetime as dt
import warnings
from typing import Tuple

import pandas as pd
Expand All @@ -14,31 +13,11 @@


def is_peak_hour(i: pd.DatetimeIndex) -> pd.Series:
"""
Calculate if a timestamp is in a peak period or not.
Parameters
----------
i : pd.DatetimeIndex
Timestamps for which to calculate if it falls in a peak period.
More precisely: if timestamp lies in one of the (left-closed) time intervals that
define the peak hour periods.
Returns
-------
Series
with boolean values and same index.
"""
if isinstance(i, pd.Timestamp):
raise TypeError("no longer supported")
warnings.warn(
"Calling this function with a single timestamp is deprecated and will be removed in a future version",
FutureWarning,
)
return is_peak_hour(pd.DatetimeIndex([i], freq="15T"))[0]

return germanpower_peakfn(i)
raise DeprecationWarning(
"``pf.is_peak_hour`` has been deprecated and will be removed in a future version."
" Use ``pf.germanpower_peakfn`` instead, or create your own peak function with"
" ``pf.create_peakfn()``."
)


def delivery_period(
Expand All @@ -59,7 +38,7 @@ def delivery_period(
front_count : int, optional (default: 1)
1 = next/coming (full) period, 2 = period after that, etc.
start_of_day : dt.time, optional (default: midnight)
Start of day for delivery periods with a longer-than-daily frequency.
Start of day for delivery periods with a daily-or-longer frequency.
Returns
-------
Expand Down
21 changes: 10 additions & 11 deletions portfolyo/tools2/intersect.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from typing import List

from ..tools.intersect import indices_flex
from .types import NDFrameLike
from ..tools import intersect as tools_intersect
from .types import Indexable


def indexable(
*frames: NDFrameLike,
*objs: Indexable,
ignore_freq: bool = False,
ignore_tz: bool = False,
ignore_start_of_day: bool = False,
) -> List[NDFrameLike]:
) -> List[Indexable]:
"""Intersect several dataframes and/or series.
Parameters
----------
*frames : pd.Series and/or pd.DataFrame and/or PfLines and/or PfStates
The frames to intersect.
*objs : pd.Series and/or pd.DataFrame and/or PfLines and/or PfStates
The indexable objects to intersect.
ignore_freq: bool, optional (default: False)
If True, do the intersection even if the frequencies do not match; drop the
time periods that do not (fully) exist in either of the frames.
Expand All @@ -28,18 +28,17 @@ def indexable(
Returns
-------
list of series and/or dataframes
As input, but trimmed to their intersection.
As input, but trimmed to their intersection.
Notes
-----
The indices must have equal frequency, timezone, start-of-day. Otherwise, an error
is raised. If there is no overlap, empty frames are returned.
"""
new_idxs = indices_flex(
*[fr.index for fr in frames],
new_idxs = tools_intersect.indices_flex(
*[o.index for o in objs],
ignore_freq=ignore_freq,
ignore_tz=ignore_tz,
ignore_start_of_day=ignore_start_of_day,
)
return [fr.loc[idx] for idx, fr in zip(new_idxs, frames)]
return [o.loc[i] for i, o in zip(new_idxs, objs)]
4 changes: 3 additions & 1 deletion portfolyo/tools2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
from ..core.pfline import PfLine
from ..core.pfstate import PfState

NDFrameLike = TypeVar("NDFrameLike", pd.Series, pd.DataFrame, PfLine, PfState)
Indexable = TypeVar(
"Series_DataFrame_PfLine_PfState", pd.Series, pd.DataFrame, PfLine, PfState
)

0 comments on commit aa972de

Please sign in to comment.