added limit parameter to closest lookups
This commit is contained in:
parent
336276cf4b
commit
0cbfede7b6
@ -126,11 +126,12 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
as_on_match: str = "closest",
|
as_on_match: str = "closest",
|
||||||
prior_match: str = "closest",
|
prior_match: str = "closest",
|
||||||
closest: Literal["previous", "next", "exact"] = 'previous',
|
closest: Literal["previous", "next", "exact"] = 'previous',
|
||||||
|
closest_max_days: int = -1,
|
||||||
if_not_found: Literal['fail', 'nan'] = 'fail',
|
if_not_found: Literal['fail', 'nan'] = 'fail',
|
||||||
compounding: bool = True,
|
compounding: bool = True,
|
||||||
interval_type: Literal['years', 'months', 'days'] = 'years',
|
interval_type: Literal['years', 'months', 'days'] = 'years',
|
||||||
interval_value: int = 1,
|
interval_value: int = 1,
|
||||||
date_format: str = None
|
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
|
||||||
|
|
||||||
@ -172,6 +173,12 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
Should be passed as a datetime library compatible string.
|
Should be passed as a datetime library compatible string.
|
||||||
Sets the date format only for this operation. To set it globally, use FincalOptions.date_format
|
Sets the date format only for this operation. To set it globally, use FincalOptions.date_format
|
||||||
|
|
||||||
|
closest_max_days: int, default -1
|
||||||
|
The maximum acceptable gap between the provided date arguments and actual date.
|
||||||
|
Pass -1 for no limit.
|
||||||
|
Note: There's a hard max limit of 1000 days due to Python's limits on recursion.
|
||||||
|
This can be overridden by importing the sys module.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
A tuple containing the date and float value of the returns.
|
A tuple containing the date and float value of the returns.
|
||||||
@ -191,8 +198,8 @@ class TimeSeries(TimeSeriesCore):
|
|||||||
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)
|
||||||
|
|
||||||
prev_date = as_on - relativedelta(**{interval_type: interval_value})
|
prev_date = as_on - relativedelta(**{interval_type: interval_value})
|
||||||
current = _find_closest_date(self.data, as_on, as_on_delta, if_not_found)
|
current = _find_closest_date(self.data, as_on, closest_max_days, as_on_delta, if_not_found)
|
||||||
previous = _find_closest_date(self.data, prev_date, prior_delta, if_not_found)
|
previous = _find_closest_date(self.data, prev_date, closest_max_days, prior_delta, if_not_found)
|
||||||
|
|
||||||
if current[1] == str('nan') or previous[1] == str('nan'):
|
if current[1] == str('nan') or previous[1] == str('nan'):
|
||||||
return as_on, float('NaN')
|
return as_on, float('NaN')
|
||||||
|
@ -85,7 +85,7 @@ def _preprocess_match_options(as_on_match: str, prior_match: str, closest: str)
|
|||||||
return as_on_delta, prior_delta
|
return as_on_delta, prior_delta
|
||||||
|
|
||||||
|
|
||||||
def _find_closest_date(data, date, delta, if_not_found):
|
def _find_closest_date(data, date, limit_days, delta, if_not_found):
|
||||||
"""Helper function to find data for the closest available date"""
|
"""Helper function to find data for the closest available date"""
|
||||||
|
|
||||||
if delta.days < 0 and date < min(data):
|
if delta.days < 0 and date < min(data):
|
||||||
@ -97,8 +97,8 @@ def _find_closest_date(data, date, delta, if_not_found):
|
|||||||
if row is not None:
|
if row is not None:
|
||||||
return date, row
|
return date, row
|
||||||
|
|
||||||
if delta:
|
if delta and limit_days != 0:
|
||||||
return _find_closest_date(data, date + delta, delta, if_not_found)
|
return _find_closest_date(data, date + delta, limit_days-1, delta, if_not_found)
|
||||||
|
|
||||||
if if_not_found == "fail":
|
if if_not_found == "fail":
|
||||||
raise DateNotFoundError("Data not found for date", date)
|
raise DateNotFoundError("Data not found for date", date)
|
||||||
|
Loading…
Reference in New Issue
Block a user