diff --git a/pvanalytics/util/_fit.py b/pvanalytics/util/_fit.py index 65a61559..eb33f2a2 100644 --- a/pvanalytics/util/_fit.py +++ b/pvanalytics/util/_fit.py @@ -127,4 +127,8 @@ def _quartic(x, a, b, c, e): ) model = _quartic(x, params[0], params[1], params[2], params[3]) residuals = y - model - return 1 - (np.sum(residuals**2) / np.sum((y - np.mean(y))**2)) + ss_res = np.sum(residuals**2) + ss_tot = np.sum((y - np.mean(y))**2) + if ss_tot == 0: + return 0.0 + return 1 - (ss_res / ss_tot) diff --git a/pvanalytics/util/_functions.py b/pvanalytics/util/_functions.py index 455dde35..90e2cebe 100644 --- a/pvanalytics/util/_functions.py +++ b/pvanalytics/util/_functions.py @@ -14,5 +14,11 @@ def freq_to_timedelta(freq): ------- Timedelta Timedelta corresponding to a single period at `freq`. + For offsets that cannot be directly converted (e.g. 'D', 'W'), + falls back to using the offset's nanosecond representation. """ - return pd.to_timedelta(frequencies.to_offset(freq)) + offset = frequencies.to_offset(freq) + try: + return pd.to_timedelta(offset) + except ValueError: + return pd.Timedelta(offset.nanos, unit='ns')