diff --git a/fincal/fincal.py b/fincal/fincal.py index 4d1bd2e..b0fe12a 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -266,7 +266,8 @@ class TimeSeries(TimeSeriesCore): prev_date = as_on - relativedelta(**{interval_type: interval_value}) 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, closest_max_days, prior_delta, if_not_found) + if current[1] != str("nan"): + 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"): return as_on, float("NaN") diff --git a/tests/test_fincal.py b/tests/test_fincal.py index c2a6f37..1b9489e 100644 --- a/tests/test_fincal.py +++ b/tests/test_fincal.py @@ -233,3 +233,8 @@ class TestReturns: returns1 = ts.calculate_returns("2020-04-10", interval_type='days', interval_value=90, date_format='%Y-%m-%d') returns2 = ts.calculate_returns("04-10-2020", interval_type='days', interval_value=90) assert round(returns1[1], 4) == round(returns2[1], 4) == 5.727 + + def test_limits(self): + ts = TimeSeries(self.data, frequency='M') + with pytest.raises(DateNotFoundError): + ts.calculate_returns("2020-04-25", interval_type='days', interval_value=90, closest_max_days=10)