Implemented DateOutOfRange error in find closes date

This commit is contained in:
Gourav Kumar 2022-02-26 20:42:27 +05:30
parent c9bfa485f5
commit 336276cf4b
2 changed files with 7 additions and 2 deletions

View File

@ -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',

View File

@ -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