Skip to content

Commit 0e34d21

Browse files
authored
Merge pull request #41 from harp-tech/empty-datetime-epoch
Ensure datetime index when epoch is specified
2 parents 7f10063 + 3fdb2f6 commit 0e34d21

4 files changed

Lines changed: 16 additions & 2 deletions

File tree

harp/io.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ def _fromraw(
132132
keep_type: bool = False,
133133
):
134134
if len(data) == 0:
135-
return pd.DataFrame(columns=columns, index=pd.Index([], dtype=np.float64, name="Time"))
135+
return pd.DataFrame(
136+
columns=columns,
137+
index=pd.DatetimeIndex([], name="Time")
138+
if epoch
139+
else pd.Index([], dtype=np.float64, name="Time"),
140+
)
136141

137142
if address is not None and address != data[2]:
138143
raise ValueError(f"expected address {address} but got {data[2]}")

tests/data/empty_0.bin

Whitespace-only changes.

tests/params.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from datetime import datetime
23
from os import PathLike
34
from pathlib import Path
45
from typing import Iterable, Optional, Type, Union
@@ -20,6 +21,7 @@ class DataFileParam:
2021
expected_length: Optional[int] = None
2122
expected_error: Optional[Type[BaseException]] = None
2223
repeat_data: Optional[int] = None
24+
epoch: Optional[datetime] = None
2325
keep_type: bool = False
2426

2527
def __post_init__(self):

tests/test_io.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from contextlib import nullcontext
22

33
import numpy as np
4+
import pandas as pd
45
import pytest
56
from pytest import mark
67

7-
from harp.io import MessageType, format, parse, read
8+
from harp.io import REFERENCE_EPOCH, MessageType, format, parse, read
89
from tests.params import DataFileParam
910

1011
testdata = [
@@ -30,6 +31,9 @@
3031
DataFileParam(path="data/write_0.bin", expected_address=0, expected_rows=4),
3132
DataFileParam(path="data/write_0.bin", expected_address=0, expected_rows=4, keep_type=True),
3233
DataFileParam(path="data/device_0.bin", expected_rows=300, repeat_data=300),
34+
DataFileParam(path="data/device_0.bin", expected_rows=1, epoch=REFERENCE_EPOCH),
35+
DataFileParam(path="data/empty_0.bin", expected_rows=0, epoch=REFERENCE_EPOCH),
36+
DataFileParam(path="data/empty_0.bin", expected_rows=0),
3337
]
3438

3539

@@ -46,6 +50,7 @@ def test_read(dataFile: DataFileParam):
4650
address=dataFile.expected_address,
4751
dtype=dataFile.expected_dtype,
4852
length=dataFile.expected_length,
53+
epoch=dataFile.epoch,
4954
keep_type=dataFile.keep_type,
5055
)
5156
else:
@@ -54,9 +59,11 @@ def test_read(dataFile: DataFileParam):
5459
address=dataFile.expected_address,
5560
dtype=dataFile.expected_dtype,
5661
length=dataFile.expected_length,
62+
epoch=dataFile.epoch,
5763
keep_type=dataFile.keep_type,
5864
)
5965
assert len(data) == dataFile.expected_rows
66+
assert isinstance(data.index, pd.DatetimeIndex if dataFile.epoch else pd.Index)
6067
if dataFile.keep_type:
6168
assert MessageType.__name__ in data.columns and data[MessageType.__name__].dtype == "category"
6269

0 commit comments

Comments
 (0)