diff --git a/fincal/core.py b/fincal/core.py index 4cd8836..4f7b698 100644 --- a/fincal/core.py +++ b/fincal/core.py @@ -43,6 +43,23 @@ class _IndexSlicer: return item +def date_parser(*params): + def parse_dates(func): + def wrapper_func(*args, **kwargs): + date_format = kwargs.get('date_format', None) + + for i, j in enumerate(params): + date = kwargs.get(j, None) + if date is None: + date = args[i+1] + + parsed_date = _parse_date(date, date_format) + kwargs[j] = parsed_date + return func(**kwargs) + return wrapper_func + return parse_dates + + class Series(UserList): """Container for a series of objects, all objects must be of the same type""" @@ -157,7 +174,7 @@ class TimeSeriesCore(UserDict): def __init__( self, data: List[Iterable], frequency: Literal["D", "W", "M", "Q", "H", "Y"], date_format: str = "%Y-%m-%d" ): - """Instantiate a TimeSeries object + """Instantiate a TimeSeriesCore object Parameters ---------- diff --git a/fincal/fincal.py b/fincal/fincal.py index 7c84cf5..2288752 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -5,7 +5,7 @@ from typing import Iterable, List, Literal, Mapping, Union from dateutil.relativedelta import relativedelta -from .core import AllFrequencies, TimeSeriesCore +from .core import AllFrequencies, TimeSeriesCore, date_parser from .utils import ( _find_closest_date, _interval_to_years, @@ -184,6 +184,7 @@ class TimeSeries(TimeSeriesCore): return self.__class__(new_ts, frequency=self.frequency.symbol) + # @date_parser('as_on') def calculate_returns( self, as_on: Union[str, datetime.datetime], @@ -276,6 +277,7 @@ class TimeSeries(TimeSeriesCore): returns = returns ** (1 / years) return (current[0] if return_actual_date else as_on), returns - 1 + # @date_parser('from_date', 'to_date') def calculate_rolling_returns( self, from_date: Union[datetime.date, str],