Skip to content

Commit b0de4d9

Browse files
committed
Support None or int with datetime index in ring buffer
1 parent 115d911 commit b0de4d9

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,12 @@ def window(
344344
if self.count_covered() == 0:
345345
return np.array([]) if isinstance(self._buffer, np.ndarray) else []
346346

347-
# If both are indices or None convert to datetime
348-
if not isinstance(start, datetime) and not isinstance(end, datetime):
349-
start, end = self._to_covered_indices(start, end)
347+
# If there is an int index or None convert to datetime
348+
if not isinstance(start, datetime):
349+
start, _ = self._to_covered_indices(start, -1)
350350
start = self.get_timestamp(start)
351+
if not isinstance(end, datetime):
352+
_, end = self._to_covered_indices(0, end)
351353
end = self.get_timestamp(end)
352354

353355
# Here we should have both as datetime

tests/timeseries/test_ringbuffer.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,11 @@ def test_window_datetime() -> None:
525525
# Test datetime and Nones
526526
win = buffer.window(None, None)
527527
assert [0, np.nan, 2, 3, 4] == list(win)
528-
with pytest.raises(ValueError):
529-
win = buffer.window(dt(0), None)
530-
#assert [0, np.nan, 2, 3, 4] == list(win) # should not raise
531-
with pytest.raises(ValueError):
532-
win = buffer.window(None, dt(4))
533-
#assert [0, np.nan, 2, 3, 4] == list(win) # should not raise
528+
win = buffer.window(dt(0), None)
529+
assert [0, np.nan, 2, 3, 4] == list(win)
530+
win = buffer.window(None, dt(5))
531+
assert [0, np.nan, 2, 3, 4] == list(win)
532+
534533
win = buffer.window(dt(0), dt(3), force_copy=False, fill_value=None)
535534
assert [0, np.nan, 2] == list(win)
536535
buffer._buffer[1] = 1 # pylint: disable=protected-access
@@ -606,15 +605,6 @@ def test_window_index_fill_value() -> None:
606605
def test_window_fail() -> None:
607606
"""Test the window function with invalid arguments."""
608607
buffer = get_orb([0.0, 1.0, 2.0, 3.0, 4.0])
609-
# Go crazy with the indices
610-
with pytest.raises(IndexError):
611-
buffer.window(dt(1), 3)
612-
with pytest.raises(IndexError):
613-
buffer.window(1, dt(3))
614-
with pytest.raises(IndexError):
615-
buffer.window(None, dt(2))
616-
with pytest.raises(IndexError):
617-
buffer.window(dt(2), None)
618608
# Invalid argument combination
619609
with pytest.raises(ValueError):
620610
buffer.window(0, 1, force_copy=False, fill_value=0)

0 commit comments

Comments
 (0)