From 698d3540f3ec79b4db76ab24395cdcdb6e818f7a Mon Sep 17 00:00:00 2001 From: Sjoerd Geevers Date: Fri, 15 Nov 2024 09:21:35 +0100 Subject: [PATCH] timeseries: update fill nans for case of all nans Update fill_nan_in_timeseries for the case where the timeseries has only nans. In this case, the result should also only be nans. --- src/rtctools/data/timeseries.py | 2 ++ tests/data/test_timeseries.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/rtctools/data/timeseries.py b/src/rtctools/data/timeseries.py index a6cbc4c..4d1bd0a 100644 --- a/src/rtctools/data/timeseries.py +++ b/src/rtctools/data/timeseries.py @@ -82,5 +82,7 @@ def fill_nan_in_timeseries(times: List[datetime], values: np.ndarray, interp_arg interp_args = {} times_sec = np.array([(t - times[0]).total_seconds() for t in times]) nans = np.isnan(values) + if all(nans): + return values result = np.interp(times_sec, times_sec[~nans], values[~nans], **interp_args) return result diff --git a/tests/data/test_timeseries.py b/tests/data/test_timeseries.py index 54047b6..82308e3 100644 --- a/tests/data/test_timeseries.py +++ b/tests/data/test_timeseries.py @@ -65,3 +65,10 @@ def test_fill_nan_in_timeseries(self): result = fill_nan_in_timeseries(times, values) ref_result = np.array([1.0, 1.0, 2.0, 3.0, 3.0]) np.testing.assert_almost_equal(result, ref_result) + values = np.array([np.nan, 1.0, np.nan, np.nan, np.nan]) + result = fill_nan_in_timeseries(times, values) + ref_result = np.array([1.0, 1.0, 1.0, 1.0, 1.0]) + np.testing.assert_almost_equal(result, ref_result) + values = np.array([np.nan, np.nan, np.nan, np.nan, np.nan]) + result = fill_nan_in_timeseries(times, values) + assert all(np.isnan(result))