From 0fec9abac03f2ba900ef3cfe1614c7822452e1c0 Mon Sep 17 00:00:00 2001 From: Gourav Kumar Date: Sun, 11 Sep 2022 15:10:12 +0530 Subject: [PATCH] handled nan in calculate returns and SD --- pyfacts/pyfacts.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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