changed find_closest_date input from dict to TimeSeries

This commit is contained in:
Gourav Kumar 2022-07-24 08:52:30 +05:30
parent c4e1d8b586
commit c35bc35529
2 changed files with 11 additions and 7 deletions

View File

@ -327,10 +327,10 @@ 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)
current = _find_closest_date(self.data, as_on, closest_max_days, as_on_delta, if_not_found) current = _find_closest_date(self, as_on, closest_max_days, as_on_delta, if_not_found)
prev_date = as_on - relativedelta(**{return_period_unit: return_period_value}) prev_date = as_on - relativedelta(**{return_period_unit: return_period_value})
if current[1] != str("nan"): if current[1] != str("nan"):
previous = _find_closest_date(self.data, prev_date, closest_max_days, prior_delta, if_not_found) previous = _find_closest_date(self, 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")

View File

@ -166,7 +166,7 @@ def _preprocess_from_to_date(
if from_date is None: if from_date is None:
expected_start_date = time_series.start_date + relativedelta(**{return_period_unit: return_period_value}) expected_start_date = time_series.start_date + relativedelta(**{return_period_unit: return_period_value})
from_date = _find_closest_date(time_series.data, expected_start_date, 999, as_on_match, "fail")[0] from_date = _find_closest_date(time_series, expected_start_date, 999, as_on_match, "fail")[0]
if to_date is None: if to_date is None:
to_date = time_series.end_date to_date = time_series.end_date
@ -181,16 +181,20 @@ def _find_closest_date(
delta: datetime.timedelta, delta: datetime.timedelta,
if_not_found: Literal["fail", "nan"], if_not_found: Literal["fail", "nan"],
) -> Tuple[datetime.datetime, float]: ) -> Tuple[datetime.datetime, float]:
"""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): data:
TimeSeries data
"""
if delta.days < 0 and date < min(data.data):
raise DateOutOfRangeError(date, "min") raise DateOutOfRangeError(date, "min")
if delta.days > 0 and date > max(data): if delta.days > 0 and date > max(data.data):
raise DateOutOfRangeError(date, "max") raise DateOutOfRangeError(date, "max")
row: tuple = data.get(date, None) row: tuple = data.get(date, None)
if row is not None: if row is not None:
return date, row return row
if delta and limit_days != 0: if delta and limit_days != 0:
return _find_closest_date(data, date + delta, limit_days - 1, delta, if_not_found) return _find_closest_date(data, date + delta, limit_days - 1, delta, if_not_found)