date parsing replaced with _parse_date function

This is to avoid ambiguity and keep date parsing uniform
This commit is contained in:
Gourav Kumar 2022-02-21 13:11:19 +05:30
parent cbace875c1
commit 44d5ea7b29

View File

@ -5,7 +5,7 @@ from typing import List, Union
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from .core import AllFrequencies, TimeSeriesCore, _preprocess_match_options from .core import AllFrequencies, TimeSeriesCore, _parse_date, _preprocess_match_options
def create_date_series( def create_date_series(
@ -113,12 +113,13 @@ class TimeSeries(TimeSeriesCore):
def calculate_returns( def calculate_returns(
self, self,
as_on: datetime.datetime, as_on: Union[str, datetime.datetime],
as_on_match: str = "closest", as_on_match: str = "closest",
prior_match: str = "closest", prior_match: str = "closest",
closest: str = "previous", closest: str = "previous",
compounding: bool = True, compounding: bool = True,
years: int = 1, years: int = 1,
date_format: str = None
) -> float: ) -> float:
"""Method to calculate returns for a certain time-period as on a particular date """Method to calculate returns for a certain time-period as on a particular date
@ -158,6 +159,7 @@ class TimeSeries(TimeSeriesCore):
>>> calculate_returns(datetime.date(2020, 1, 1), years=1) >>> calculate_returns(datetime.date(2020, 1, 1), years=1)
""" """
as_on = _parse_date(as_on, date_format)
as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest) as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest)
while True: while True:
@ -184,17 +186,24 @@ class TimeSeries(TimeSeriesCore):
def calculate_rolling_returns( def calculate_rolling_returns(
self, self,
from_date: datetime.date, from_date: Union[datetime.date, str],
to_date: datetime.date, to_date: Union[datetime.date, str],
frequency: str = "D", frequency: str = None,
as_on_match: str = "closest", as_on_match: str = "closest",
prior_match: str = "closest", prior_match: str = "closest",
closest: str = "previous", closest: str = "previous",
compounding: bool = True, compounding: bool = True,
years: int = 1, years: int = 1,
date_format: str = None
) -> List[tuple]: ) -> List[tuple]:
"""Calculates the rolling return""" """Calculates the rolling return"""
from_date = _parse_date(from_date, date_format)
to_date = _parse_date(to_date, date_format)
if frequency is None:
frequency = self.frequency
else:
try: try:
frequency = getattr(AllFrequencies, frequency) frequency = getattr(AllFrequencies, frequency)
except AttributeError: except AttributeError: