diff --git a/opengrid/library/analysis.py b/opengrid/library/analysis.py index daea53a..ff2dadb 100644 --- a/opengrid/library/analysis.py +++ b/opengrid/library/analysis.py @@ -105,3 +105,33 @@ def count_peaks(ts): result = on_toggles & shifted count = result.sum() return count + + +def load_factor(ts, resolution=None, norm=None): + """ + Calculate the ratio of input vs. norm over a given interval. + + Parameters + ---------- + ts : pandas.Series + timeseries + resolution : str, optional + interval over which to calculate the ratio + default: resolution of the input timeseries + norm : int | float, optional + denominator of the ratio + default: the maximum of the input timeseries + + Returns + ------- + pandas.Series + """ + if norm is None: + norm = ts.max() + + if resolution is not None: + ts = ts.resample(rule=resolution).mean() + + lf = ts / norm + + return lf diff --git a/opengrid/tests/test_analyses.py b/opengrid/tests/test_analyses.py index 4fc903e..e5568ba 100644 --- a/opengrid/tests/test_analyses.py +++ b/opengrid/tests/test_analyses.py @@ -28,6 +28,17 @@ def test_count_peaks(self): count = og.analysis.count_peaks(ts) self.assertEqual(count, 13) + def test_load_factor(self): + ts = og.datasets.get('electricity_2016_hour') + ts = ts['e1de'].truncate(after=pd.Timestamp('20160107')) + lf1 = og.analysis.load_factor(ts) + self.assertIsInstance(ts, pd.Series) + self.assertAlmostEqual(ts.iloc[0], (lf1 * ts.max()).iloc[0]) + + lf2 = og.analysis.load_factor(ts, resolution='3h', norm=800) + self.assertIsInstance(ts, pd.Series) + self.assertAlmostEqual(175.0345212009457, (lf2 * 800).iloc[0]) + if __name__ == '__main__': unittest.main()