diff --git a/backdrop/core/response.py b/backdrop/core/response.py index 01bc6b11..433cdbba 100644 --- a/backdrop/core/response.py +++ b/backdrop/core/response.py @@ -152,6 +152,9 @@ def fill_missing_periods(self, start_date, end_date, collect=None): def amount_to_shift(self, delta): is_reversed = delta < 0 + if len(self._data) == 0: + return 0 + return min([ first_nonempty(i['values'], is_reversed) for i in self._data], key=abs) diff --git a/tests/read/test_weekly_grouped_data.py b/tests/read/test_weekly_grouped_data.py index 669ce297..c3ecfb20 100644 --- a/tests/read/test_weekly_grouped_data.py +++ b/tests/read/test_weekly_grouped_data.py @@ -86,3 +86,7 @@ def test_filling_data_for_missing_weeks(self): has_entry("_start_at", d_tz(2013, 4, 8)), has_entry("_start_at", d_tz(2013, 4, 15)) )) + + def test_with_empty_data(self): + data = PeriodGroupedData([], WEEK) + assert_that(data.amount_to_shift(7), equal_to(0))