diff --git a/pyfacts/pyfacts.py b/pyfacts/pyfacts.py index 9eb1557..51e955c 100644 --- a/pyfacts/pyfacts.py +++ b/pyfacts/pyfacts.py @@ -328,11 +328,17 @@ class TimeSeries(TimeSeriesCore): as_on_delta, prior_delta = _preprocess_match_options(as_on_match, prior_match, closest) 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, 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") + or current[0] == str("nan") + or previous[0] == str("nan") + ): return as_on, float("NaN") returns = current[1] / previous[1] @@ -593,7 +599,7 @@ class TimeSeries(TimeSeriesCore): kwargs["to_date"] = kwargs.get("to_date", self.end_date) rr = self.calculate_rolling_returns(**kwargs) - mean_rr = statistics.mean(rr.values) + mean_rr = statistics.mean(filter(lambda x: str(x) != "nan", rr.values)) if annualise_returns: mean_rr = (1 + mean_rr) ** (1 / years) - 1