From 336276cf4b41941614809673f44ec8e43b9af6eb Mon Sep 17 00:00:00 2001 From: gouravkr Date: Sat, 26 Feb 2022 20:42:27 +0530 Subject: [PATCH] Implemented DateOutOfRange error in find closes date --- fincal/fincal.py | 2 +- fincal/utils.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fincal/fincal.py b/fincal/fincal.py index 036a3cd..862119e 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -125,7 +125,7 @@ class TimeSeries(TimeSeriesCore): return_actual_date: bool = True, as_on_match: str = "closest", prior_match: str = "closest", - closest: str = "previous", + closest: Literal["previous", "next", "exact"] = 'previous', if_not_found: Literal['fail', 'nan'] = 'fail', compounding: bool = True, interval_type: Literal['years', 'months', 'days'] = 'years', diff --git a/fincal/utils.py b/fincal/utils.py index 097199c..e716c2d 100644 --- a/fincal/utils.py +++ b/fincal/utils.py @@ -2,7 +2,7 @@ import datetime from dataclasses import dataclass from typing import Iterable, List, Literal, Mapping, Sequence, Tuple, Union -from .exceptions import DateNotFoundError +from .exceptions import DateNotFoundError, DateOutOfRangeError @dataclass @@ -88,6 +88,11 @@ def _preprocess_match_options(as_on_match: str, prior_match: str, closest: str) def _find_closest_date(data, date, delta, if_not_found): """Helper function to find data for the closest available date""" + if delta.days < 0 and date < min(data): + raise DateOutOfRangeError(date, 'min') + if delta.days > 0 and date > max(data): + raise DateOutOfRangeError(date, 'max') + row = data.get(date, None) if row is not None: return date, row