handled nan in calculate returns and SD

This commit is contained in:
Gourav Kumar 2022-09-11 15:10:12 +05:30
parent c35bc35529
commit 0fec9abac0

View File

@ -328,11 +328,17 @@ 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, 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, 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")
or current[0] == str("nan")
or previous[0] == str("nan")
):
return as_on, float("NaN") return as_on, float("NaN")
returns = current[1] / previous[1] returns = current[1] / previous[1]
@ -593,7 +599,7 @@ class TimeSeries(TimeSeriesCore):
kwargs["to_date"] = kwargs.get("to_date", self.end_date) kwargs["to_date"] = kwargs.get("to_date", self.end_date)
rr = self.calculate_rolling_returns(**kwargs) 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: if annualise_returns:
mean_rr = (1 + mean_rr) ** (1 / years) - 1 mean_rr = (1 + mean_rr) ** (1 / years) - 1