From c35bc35529dc298ea997c69087256d0fa972da50 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 24 Jul 2022 08:52:30 +0530 Subject: [PATCH] changed find_closest_date input from dict to TimeSeries --- pyfacts/pyfacts.py | 4 ++-- pyfacts/utils.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pyfacts/pyfacts.py b/pyfacts/pyfacts.py index 2177067..9eb1557 100644 --- a/pyfacts/pyfacts.py +++ b/pyfacts/pyfacts.py @@ -327,10 +327,10 @@ class TimeSeries(TimeSeriesCore): 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}) 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"): return as_on, float("NaN") diff --git a/pyfacts/utils.py b/pyfacts/utils.py index 84e32d1..f212101 100644 --- a/pyfacts/utils.py +++ b/pyfacts/utils.py @@ -166,7 +166,7 @@ def _preprocess_from_to_date( if from_date is None: 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: to_date = time_series.end_date @@ -181,16 +181,20 @@ def _find_closest_date( delta: datetime.timedelta, if_not_found: Literal["fail", "nan"], ) -> 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") - if delta.days > 0 and date > max(data): + if delta.days > 0 and date > max(data.data): raise DateOutOfRangeError(date, "max") row: tuple = data.get(date, None) if row is not None: - return date, row + return row if delta and limit_days != 0: return _find_closest_date(data, date + delta, limit_days - 1, delta, if_not_found)