Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
implement resample by calendar offset
Browse files Browse the repository at this point in the history
  • Loading branch information
peerchemist committed May 8, 2019
1 parent 45e19fc commit c31fd7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions finta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def resample(df: pd.DataFrame, interval: str) -> pd.DataFrame:
return df.resample(interval).agg(d)


def resample_calendar(df: pd.DataFrame, offset: str) -> pd.DataFrame:
"""Resample the DataFrame by calendar offset.
See http://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#anchored-offsets for compatible offsets.
:param df: data
:param offset: calendar offset
:return: result DataFrame
"""

d = {"open": "first", "high": "max", "low": "min", "close": "last", "volume": "sum"}

return df.resample(offset).agg(d)


def trending_up(df: pd.Series, period: int) -> pd.Series:
"""returns boolean Series if the inputs Series is trending up over last n periods.
:param df: data
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def test_resample():
]


def test_resample_calendar():

df = to_dataframe(data)
assert isinstance(resample(df, "W-Mon"), DataFrame)
assert list(resample(df, "W-Mon").index.values[-2:]) == [
numpy.datetime64("2019-05-06T00:00:00.000000000"),
numpy.datetime64("2019-05-13T00:00:00.000000000"),
]


def test_trending_up():

df = to_dataframe(data)
Expand Down

0 comments on commit c31fd7d

Please sign in to comment.