From ec5fb7f8ff6a3ed6b92151cd52440a2b9c65c4df Mon Sep 17 00:00:00 2001 From: Ian Later Date: Wed, 28 Oct 2026 13:01:20 -0700 Subject: [PATCH 1/6] python(fix): return run bounds as nanosecond timestamps A time axis built from Run.start_time used microsecond integers while channel data uses nanoseconds, so interpolation produced NaN. --- .../+run-timestamp-resolution.fix.md | 1 + .../sift_client/_tests/sift_types/test_run.py | 54 +++++++++++++++++++ python/lib/sift_client/sift_types/run.py | 13 +++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 python/changelog.d/+run-timestamp-resolution.fix.md diff --git a/python/changelog.d/+run-timestamp-resolution.fix.md b/python/changelog.d/+run-timestamp-resolution.fix.md new file mode 100644 index 0000000000..064d53b008 --- /dev/null +++ b/python/changelog.d/+run-timestamp-resolution.fix.md @@ -0,0 +1 @@ +`Run.start_time` and `Run.stop_time` are nanosecond timestamps, matching channel data, so a time axis built from a run lines up with `get_data`. diff --git a/python/lib/sift_client/_tests/sift_types/test_run.py b/python/lib/sift_client/_tests/sift_types/test_run.py index 58695efdf1..3f845a54ae 100644 --- a/python/lib/sift_client/_tests/sift_types/test_run.py +++ b/python/lib/sift_client/_tests/sift_types/test_run.py @@ -3,7 +3,10 @@ from datetime import datetime, timezone from unittest.mock import MagicMock +import pandas as pd import pytest +from google.protobuf.timestamp_pb2 import Timestamp +from sift.runs.v2.runs_pb2 import Run as RunProto from sift_client.sift_types import Run from sift_client.sift_types.run import RunCreate, RunUpdate @@ -134,6 +137,57 @@ def mock_run(mock_client): class TestRun: """Unit tests for Run model - tests properties and methods.""" + def test_bounds_match_channel_timestamp_resolution(self): + """Run bounds are nanosecond timestamps, matching channel data. + + ``stop_time`` is an exact second. That is the case where a + microsecond ``datetime`` and a nanosecond timestamp print the + same and still disagree as integers. + """ + start = Timestamp(seconds=1735689600, nanos=123456789) + stop = Timestamp(seconds=1735689601, nanos=0) + proto = RunProto( + run_id="r", + name="n", + description="", + created_by_user_id="u", + modified_by_user_id="u", + organization_id="o", + ) + proto.created_date.FromDatetime(datetime(2025, 1, 1, tzinfo=timezone.utc)) + proto.modified_date.CopyFrom(proto.created_date) + proto.start_time.CopyFrom(start) + proto.stop_time.CopyFrom(stop) + + run = Run._from_proto(proto) + + assert isinstance(run.start_time, pd.Timestamp) + assert isinstance(run.stop_time, pd.Timestamp) + assert run.start_time.unit == "ns" + assert run.stop_time.unit == "ns" + assert run.start_time.nanosecond == 789 + assert run.stop_time.nanosecond == 0 + start_index = pd.DatetimeIndex([run.start_time]) + assert str(start_index.dtype).startswith("datetime64[ns") + assert int(start_index.asi8[0]) == 1735689600 * 1_000_000_000 + 123456789 + axis = pd.date_range(run.start_time, run.stop_time, periods=2) + assert str(axis.dtype).startswith("datetime64[ns") + assert int(axis.asi8[0]) == int(start_index.asi8[0]) + + unset = RunProto( + run_id="r", + name="n", + description="", + created_by_user_id="u", + modified_by_user_id="u", + organization_id="o", + ) + unset.created_date.CopyFrom(proto.created_date) + unset.modified_date.CopyFrom(proto.modified_date) + unbounded = Run._from_proto(unset) + assert unbounded.start_time is None + assert unbounded.stop_time is None + def test_assets_property_calls_client(self, mock_run, mock_client): """Test that assets property calls client.assets.list_ with correct parameters.""" mock_client.assets.list_.return_value = [] diff --git a/python/lib/sift_client/sift_types/run.py b/python/lib/sift_client/sift_types/run.py index b61968d7b8..342ec21cb1 100644 --- a/python/lib/sift_client/sift_types/run.py +++ b/python/lib/sift_client/sift_types/run.py @@ -7,6 +7,7 @@ from sift.runs.v2.runs_pb2 import CreateRunRequest as CreateRunRequestProto from sift.runs.v2.runs_pb2 import Run as RunProto +from sift_client._internal.time import to_timestamp_nanos from sift_client.sift_types._base import ( BaseType, MappingHelper, @@ -31,7 +32,13 @@ class Run(BaseType[RunProto, "Run"], FileAttachmentsMixin): - """Run model representing a data collection run.""" + """Run model representing a data collection run. + + ``start_time`` and ``stop_time`` are nanosecond ``pandas.Timestamp`` + values, the same resolution as channel data from ``get_data``. A time + axis built from these lines up with that data when both are read as + integers. + """ # Required fields name: str @@ -68,12 +75,12 @@ def _from_proto(cls, proto: RunProto, sift_client: SiftClient | None = None) -> modified_by_user_id=proto.modified_by_user_id, organization_id=proto.organization_id, start_time=( - proto.start_time.ToDatetime(tzinfo=timezone.utc) + to_timestamp_nanos(proto.start_time).as_unit("ns") if proto.HasField("start_time") else None ), stop_time=( - proto.stop_time.ToDatetime(tzinfo=timezone.utc) + to_timestamp_nanos(proto.stop_time).as_unit("ns") if proto.HasField("stop_time") else None ), From 1f6892806fd44a179bb9adb9c731782891995edd Mon Sep 17 00:00:00 2001 From: Ian Later Date: Wed, 28 Oct 2026 13:01:39 -0700 Subject: [PATCH 2/6] python(chore): name the changelog fragment after the pull request --- .../changelog.d/{+run-timestamp-resolution.fix.md => 857.fix.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename python/changelog.d/{+run-timestamp-resolution.fix.md => 857.fix.md} (100%) diff --git a/python/changelog.d/+run-timestamp-resolution.fix.md b/python/changelog.d/857.fix.md similarity index 100% rename from python/changelog.d/+run-timestamp-resolution.fix.md rename to python/changelog.d/857.fix.md From b579acfe74722e225f13fe6285d2a16b9c89cb9f Mon Sep 17 00:00:00 2001 From: Ian Later Date: Wed, 28 Oct 2026 14:49:08 -0700 Subject: [PATCH 3/6] python(fix): compare run bounds with the channel timestamp index --- .../sift_client/_tests/sift_types/test_run.py | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/python/lib/sift_client/_tests/sift_types/test_run.py b/python/lib/sift_client/_tests/sift_types/test_run.py index 3f845a54ae..1623cf0b62 100644 --- a/python/lib/sift_client/_tests/sift_types/test_run.py +++ b/python/lib/sift_client/_tests/sift_types/test_run.py @@ -5,9 +5,13 @@ import pandas as pd import pytest +from google.protobuf.any_pb2 import Any from google.protobuf.timestamp_pb2 import Timestamp +from sift.common.type.v1.channel_data_type_pb2 import CHANNEL_DATA_TYPE_DOUBLE +from sift.data.v2.data_pb2 import DoubleValue, DoubleValues, Metadata from sift.runs.v2.runs_pb2 import Run as RunProto +from sift_client._internal.low_level_wrappers.data import DataLowLevelClient from sift_client.sift_types import Run from sift_client.sift_types.run import RunCreate, RunUpdate @@ -134,15 +138,34 @@ def mock_run(mock_client): return run +def _channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: + """Index ``get_data`` builds for double-channel samples at these times.""" + values = DoubleValues( + metadata=Metadata( + data_type=CHANNEL_DATA_TYPE_DOUBLE, + sampled_ms=0, + channel=Metadata.Channel(name="voltage"), + ), + values=[DoubleValue(timestamp=ts, value=1.0) for ts in timestamps], + ) + packed = Any() + packed.Pack(values) + # ``Pack`` writes ``type.googleapis.com/...``. The data parser reads the + # ``sift.data`` prefix the API sends. + packed.type_url = "sift.data.v2.DoubleValues" + frame = DataLowLevelClient.try_deserialize_channel_data(packed)["voltage"] + return frame.index + + class TestRun: """Unit tests for Run model - tests properties and methods.""" def test_bounds_match_channel_timestamp_resolution(self): - """Run bounds are nanosecond timestamps, matching channel data. + """A time axis from run bounds matches the channel index for the same timestamps. - ``stop_time`` is an exact second. That is the case where a - microsecond ``datetime`` and a nanosecond timestamp print the - same and still disagree as integers. + ``stop_time`` is an exact second. That instant prints the same as a + microsecond ``datetime`` and still disagrees when both are read as + integers. """ start = Timestamp(seconds=1735689600, nanos=123456789) stop = Timestamp(seconds=1735689601, nanos=0) @@ -160,19 +183,12 @@ def test_bounds_match_channel_timestamp_resolution(self): proto.stop_time.CopyFrom(stop) run = Run._from_proto(proto) - - assert isinstance(run.start_time, pd.Timestamp) - assert isinstance(run.stop_time, pd.Timestamp) - assert run.start_time.unit == "ns" - assert run.stop_time.unit == "ns" - assert run.start_time.nanosecond == 789 - assert run.stop_time.nanosecond == 0 - start_index = pd.DatetimeIndex([run.start_time]) - assert str(start_index.dtype).startswith("datetime64[ns") - assert int(start_index.asi8[0]) == 1735689600 * 1_000_000_000 + 123456789 + channel_index = _channel_index(start, stop) axis = pd.date_range(run.start_time, run.stop_time, periods=2) - assert str(axis.dtype).startswith("datetime64[ns") - assert int(axis.asi8[0]) == int(start_index.asi8[0]) + + assert str(channel_index.dtype).startswith("datetime64[ns") + assert axis.dtype == channel_index.dtype + assert [int(value) for value in axis.asi8] == [int(value) for value in channel_index.asi8] unset = RunProto( run_id="r", From b4e251c449a3550c2b2702ebf2649f2a37d2219b Mon Sep 17 00:00:00 2001 From: Ian Later Date: Wed, 28 Oct 2026 14:52:53 -0700 Subject: [PATCH 4/6] python(fix): narrow the channel index to a DatetimeIndex --- python/lib/sift_client/_tests/sift_types/test_run.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/lib/sift_client/_tests/sift_types/test_run.py b/python/lib/sift_client/_tests/sift_types/test_run.py index 1623cf0b62..9950f9145e 100644 --- a/python/lib/sift_client/_tests/sift_types/test_run.py +++ b/python/lib/sift_client/_tests/sift_types/test_run.py @@ -154,7 +154,9 @@ def _channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: # ``sift.data`` prefix the API sends. packed.type_url = "sift.data.v2.DoubleValues" frame = DataLowLevelClient.try_deserialize_channel_data(packed)["voltage"] - return frame.index + index = frame.index + assert isinstance(index, pd.DatetimeIndex) + return index class TestRun: From ed82892354d047682477b8a3adcaaa2b83e35fa2 Mon Sep 17 00:00:00 2001 From: Ian Later Date: Fri, 30 Oct 2026 12:33:12 -0700 Subject: [PATCH 5/6] python(fix): return annotation, step, and import bounds as nanosecond timestamps --- python/changelog.d/857.fix.md | 2 +- .../_tests/resources/test_data_imports.py | 16 ++++++++ .../_tests/sift_types/channel_time.py | 39 +++++++++++++++++++ .../_tests/sift_types/test_annotation.py | 19 +++++++++ .../_tests/sift_types/test_results.py | 21 ++++++++++ .../sift_client/_tests/sift_types/test_run.py | 34 +--------------- .../lib/sift_client/sift_types/annotation.py | 19 ++++++--- .../lib/sift_client/sift_types/data_import.py | 14 +++++-- python/lib/sift_client/sift_types/run.py | 18 ++++----- .../lib/sift_client/sift_types/test_report.py | 21 +++++++--- 10 files changed, 146 insertions(+), 57 deletions(-) create mode 100644 python/lib/sift_client/_tests/sift_types/channel_time.py diff --git a/python/changelog.d/857.fix.md b/python/changelog.d/857.fix.md index 064d53b008..357c35208b 100644 --- a/python/changelog.d/857.fix.md +++ b/python/changelog.d/857.fix.md @@ -1 +1 @@ -`Run.start_time` and `Run.stop_time` are nanosecond timestamps, matching channel data, so a time axis built from a run lines up with `get_data`. +Run, annotation, test step, and data import time bounds are nanosecond timestamps, matching channel data, so a time axis built from them lines up with `get_data`. diff --git a/python/lib/sift_client/_tests/resources/test_data_imports.py b/python/lib/sift_client/_tests/resources/test_data_imports.py index f3bb9c2ea6..a14f712c14 100644 --- a/python/lib/sift_client/_tests/resources/test_data_imports.py +++ b/python/lib/sift_client/_tests/resources/test_data_imports.py @@ -9,6 +9,7 @@ import pandas as pd import pytest +from google.protobuf.timestamp_pb2 import Timestamp from sift.common.type.v1.channel_config_pb2 import ChannelConfig as ChannelConfigProto from sift.data_imports.v2.data_imports_pb2 import ( DATA_IMPORT_STATUS_FAILED, @@ -38,6 +39,7 @@ resolve_data_type_key, ) from sift_client._internal.util.time_column import infer_time_column +from sift_client._tests.sift_types.channel_time import assert_axis_matches_channel from sift_client.resources import DataImportAPI, DataImportAPIAsync from sift_client.sift_types.channel import ChannelDataType from sift_client.sift_types.data_import import ( @@ -1280,6 +1282,20 @@ def test_unset_optionals_are_none(self): assert data_import.data_start_time is None assert data_import.data_stop_time is None + def test_bounds_match_channel_timestamp_resolution(self): + """Import bounds use the same integer scale as channel data.""" + start = Timestamp(seconds=1735689600, nanos=123456789) + stop = Timestamp(seconds=1735689601, nanos=0) + proto = _data_import_proto() + proto.data_start_time.CopyFrom(start) + proto.data_stop_time.CopyFrom(stop) + + data_import = DataImport._from_proto(proto) + + assert_axis_matches_channel( + data_import.data_start_time, data_import.data_stop_time, start, stop + ) + class TestDataImportsGet: @pytest.mark.asyncio diff --git a/python/lib/sift_client/_tests/sift_types/channel_time.py b/python/lib/sift_client/_tests/sift_types/channel_time.py new file mode 100644 index 0000000000..b05b52e711 --- /dev/null +++ b/python/lib/sift_client/_tests/sift_types/channel_time.py @@ -0,0 +1,39 @@ +"""Shared check that a time axis matches the index ``get_data`` builds.""" + +import pandas as pd +from google.protobuf.any_pb2 import Any +from google.protobuf.timestamp_pb2 import Timestamp +from sift.common.type.v1.channel_data_type_pb2 import CHANNEL_DATA_TYPE_DOUBLE +from sift.data.v2.data_pb2 import DoubleValue, DoubleValues, Metadata + +from sift_client._internal.low_level_wrappers.data import DataLowLevelClient + + +def channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: + """Index ``get_data`` builds for double-channel samples at these times.""" + values = DoubleValues( + metadata=Metadata( + data_type=CHANNEL_DATA_TYPE_DOUBLE, + sampled_ms=0, + channel=Metadata.Channel(name="voltage"), + ), + values=[DoubleValue(timestamp=ts, value=1.0) for ts in timestamps], + ) + packed = Any() + packed.Pack(values) + # ``Pack`` writes ``type.googleapis.com/...``. The data parser reads the + # ``sift.data`` prefix the API sends. + packed.type_url = "sift.data.v2.DoubleValues" + frame = DataLowLevelClient.try_deserialize_channel_data(packed)["voltage"] + index = frame.index + assert isinstance(index, pd.DatetimeIndex) + return index + + +def assert_axis_matches_channel(start, stop, *timestamps: Timestamp) -> None: + """A ``date_range`` across these bounds uses the channel index scale.""" + samples = channel_index(*timestamps) + axis = pd.date_range(start, stop, periods=2) + assert str(samples.dtype).startswith("datetime64[ns") + assert axis.dtype == samples.dtype + assert [int(value) for value in axis.asi8] == [int(value) for value in samples.asi8] diff --git a/python/lib/sift_client/_tests/sift_types/test_annotation.py b/python/lib/sift_client/_tests/sift_types/test_annotation.py index 6ac3a751cb..6778e4c84e 100644 --- a/python/lib/sift_client/_tests/sift_types/test_annotation.py +++ b/python/lib/sift_client/_tests/sift_types/test_annotation.py @@ -4,9 +4,12 @@ from unittest.mock import MagicMock import pytest +from google.protobuf.timestamp_pb2 import Timestamp +from sift.annotations.v1.annotations_pb2 import Annotation as AnnotationProto from sift.channels.v3.channels_pb2 import Channel as ChannelProto from sift.common.type.v1.channel_data_type_pb2 import ChannelDataType as ChannelDataTypeProto +from sift_client._tests.sift_types.channel_time import assert_axis_matches_channel from sift_client.sift_types import Annotation from sift_client.sift_types.annotation import ( AnnotationCommentElement, @@ -249,3 +252,19 @@ def test_state_verbs_forward_to_the_resource(self, mock_annotation, mock_client) getattr(mock_client.annotations, verb).assert_called_once_with( annotation=mock_annotation ) + + +class TestAnnotationFromProto: + def test_bounds_match_channel_timestamp_resolution(self): + """Annotation bounds use the same integer scale as channel data.""" + start = Timestamp(seconds=1735689600, nanos=123456789) + stop = Timestamp(seconds=1735689601, nanos=0) + proto = AnnotationProto(annotation_id="a", name="n", description="") + proto.created_date.FromDatetime(datetime(2025, 1, 1, tzinfo=timezone.utc)) + proto.modified_date.CopyFrom(proto.created_date) + proto.start_time.CopyFrom(start) + proto.end_time.CopyFrom(stop) + + annotation = Annotation._from_proto(proto) + + assert_axis_matches_channel(annotation.start_time, annotation.end_time, start, stop) diff --git a/python/lib/sift_client/_tests/sift_types/test_results.py b/python/lib/sift_client/_tests/sift_types/test_results.py index c1530f1e7c..1b50668bcb 100644 --- a/python/lib/sift_client/_tests/sift_types/test_results.py +++ b/python/lib/sift_client/_tests/sift_types/test_results.py @@ -8,6 +8,7 @@ from unittest.mock import MagicMock, call import pytest +from google.protobuf.timestamp_pb2 import Timestamp from sift.test_reports.v1.test_reports_pb2 import ( TestMeasurement as TestMeasurementProto, ) @@ -15,6 +16,7 @@ TestStep as TestStepProto, ) +from sift_client._tests.sift_types.channel_time import assert_axis_matches_channel from sift_client.sift_types.channel import Channel, ChannelDataType from sift_client.sift_types.test_report import ( ErrorInfo, @@ -496,3 +498,22 @@ def test_step_from_proto_handles_absent_metadata(self): proto.end_time.FromDatetime(datetime.now(timezone.utc)) step = TestStep._from_proto(proto) assert step.metadata is None + + def test_bounds_match_channel_timestamp_resolution(self): + """Step bounds use the same integer scale as channel data.""" + start = Timestamp(seconds=1735689600, nanos=123456789) + stop = Timestamp(seconds=1735689601, nanos=0) + proto = TestStepProto( + test_step_id="step_abc", + test_report_id="report_789", + name="Step", + step_type=TestStepType.ACTION.value, + step_path="1", + status=TestStatus.IN_PROGRESS.value, + ) + proto.start_time.CopyFrom(start) + proto.end_time.CopyFrom(stop) + + step = TestStep._from_proto(proto) + + assert_axis_matches_channel(step.start_time, step.end_time, start, stop) diff --git a/python/lib/sift_client/_tests/sift_types/test_run.py b/python/lib/sift_client/_tests/sift_types/test_run.py index 9950f9145e..b94b59f567 100644 --- a/python/lib/sift_client/_tests/sift_types/test_run.py +++ b/python/lib/sift_client/_tests/sift_types/test_run.py @@ -3,15 +3,11 @@ from datetime import datetime, timezone from unittest.mock import MagicMock -import pandas as pd import pytest -from google.protobuf.any_pb2 import Any from google.protobuf.timestamp_pb2 import Timestamp -from sift.common.type.v1.channel_data_type_pb2 import CHANNEL_DATA_TYPE_DOUBLE -from sift.data.v2.data_pb2 import DoubleValue, DoubleValues, Metadata from sift.runs.v2.runs_pb2 import Run as RunProto -from sift_client._internal.low_level_wrappers.data import DataLowLevelClient +from sift_client._tests.sift_types.channel_time import assert_axis_matches_channel from sift_client.sift_types import Run from sift_client.sift_types.run import RunCreate, RunUpdate @@ -138,27 +134,6 @@ def mock_run(mock_client): return run -def _channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: - """Index ``get_data`` builds for double-channel samples at these times.""" - values = DoubleValues( - metadata=Metadata( - data_type=CHANNEL_DATA_TYPE_DOUBLE, - sampled_ms=0, - channel=Metadata.Channel(name="voltage"), - ), - values=[DoubleValue(timestamp=ts, value=1.0) for ts in timestamps], - ) - packed = Any() - packed.Pack(values) - # ``Pack`` writes ``type.googleapis.com/...``. The data parser reads the - # ``sift.data`` prefix the API sends. - packed.type_url = "sift.data.v2.DoubleValues" - frame = DataLowLevelClient.try_deserialize_channel_data(packed)["voltage"] - index = frame.index - assert isinstance(index, pd.DatetimeIndex) - return index - - class TestRun: """Unit tests for Run model - tests properties and methods.""" @@ -185,12 +160,7 @@ def test_bounds_match_channel_timestamp_resolution(self): proto.stop_time.CopyFrom(stop) run = Run._from_proto(proto) - channel_index = _channel_index(start, stop) - axis = pd.date_range(run.start_time, run.stop_time, periods=2) - - assert str(channel_index.dtype).startswith("datetime64[ns") - assert axis.dtype == channel_index.dtype - assert [int(value) for value in axis.asi8] == [int(value) for value in channel_index.asi8] + assert_axis_matches_channel(run.start_time, run.stop_time, start, stop) unset = RunProto( run_id="r", diff --git a/python/lib/sift_client/sift_types/annotation.py b/python/lib/sift_client/sift_types/annotation.py index 86f3d23c49..2cbb3e3570 100644 --- a/python/lib/sift_client/sift_types/annotation.py +++ b/python/lib/sift_client/sift_types/annotation.py @@ -4,6 +4,7 @@ from enum import Enum from typing import TYPE_CHECKING, ClassVar, Literal, cast +import pandas as pd # noqa: TC002 from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator from sift.annotation_logs.v1.annotation_logs_pb2 import ( AnnotationCommentBodyElement as AnnotationCommentBodyElementProto, @@ -43,6 +44,7 @@ CreateAnnotationRequest as CreateAnnotationRequestProto, ) +from sift_client._internal.time import to_timestamp_nanos from sift_client.sift_types._base import ( BaseType, MappingHelper, @@ -105,13 +107,20 @@ def _linked_channel_to_proto(channel: Channel | CalculatedChannel) -> Annotation class Annotation(BaseType[AnnotationProto, "Annotation"]): - """Annotation model representing a time range on one or more assets.""" + """Annotation model representing a time range on one or more assets. + + ``start_time`` and ``end_time`` are nanosecond timestamps, the same + resolution as channel data from ``get_data``. + """ + + # pandas.Timestamp has no pydantic schema, so it is validated with isinstance. + model_config = ConfigDict(arbitrary_types_allowed=True) # Required fields name: str description: str - start_time: datetime - end_time: datetime + start_time: pd.Timestamp | datetime + end_time: pd.Timestamp | datetime annotation_type: AnnotationType organization_id: str created_date: datetime @@ -141,8 +150,8 @@ def _from_proto( id_=proto.annotation_id, name=proto.name, description=proto.description, - start_time=proto.start_time.ToDatetime(tzinfo=timezone.utc), - end_time=proto.end_time.ToDatetime(tzinfo=timezone.utc), + start_time=to_timestamp_nanos(proto.start_time), + end_time=to_timestamp_nanos(proto.end_time), annotation_type=AnnotationType(proto.annotation_type), organization_id=proto.organization_id, created_date=proto.created_date.ToDatetime(tzinfo=timezone.utc), diff --git a/python/lib/sift_client/sift_types/data_import.py b/python/lib/sift_client/sift_types/data_import.py index 86688177bc..0cb4c83220 100644 --- a/python/lib/sift_client/sift_types/data_import.py +++ b/python/lib/sift_client/sift_types/data_import.py @@ -1436,8 +1436,14 @@ class DataImport(BaseType[DataImportProto, "DataImport"]): The format-specific import config is not modeled here; read it off ``data_import.proto`` (e.g. ``data_import.proto.csv_config``). + + ``data_start_time`` and ``data_stop_time`` are nanosecond timestamps, + the same resolution as channel data from ``get_data``. """ + # pandas.Timestamp has no pydantic schema, so it is validated with isinstance. + model_config = ConfigDict(arbitrary_types_allowed=True) + source_url: str | None status: DataImportStatus error_message: str | None @@ -1450,8 +1456,8 @@ class DataImport(BaseType[DataImportProto, "DataImport"]): report_id: str | None asset_id: str | None # Time range of the imported data, not of the import itself. - data_start_time: datetime | None - data_stop_time: datetime | None + data_start_time: pd.Timestamp | datetime | None + data_stop_time: pd.Timestamp | datetime | None @classmethod def _from_proto( @@ -1471,12 +1477,12 @@ def _from_proto( report_id=proto.report_id or None, asset_id=proto.asset_id or None, data_start_time=( - proto.data_start_time.ToDatetime(tzinfo=timezone.utc) + to_timestamp_nanos(proto.data_start_time) if proto.HasField("data_start_time") else None ), data_stop_time=( - proto.data_stop_time.ToDatetime(tzinfo=timezone.utc) + to_timestamp_nanos(proto.data_stop_time) if proto.HasField("data_stop_time") else None ), diff --git a/python/lib/sift_client/sift_types/run.py b/python/lib/sift_client/sift_types/run.py index 342ec21cb1..796db2ec42 100644 --- a/python/lib/sift_client/sift_types/run.py +++ b/python/lib/sift_client/sift_types/run.py @@ -3,7 +3,8 @@ from datetime import datetime, timedelta, timezone from typing import TYPE_CHECKING, ClassVar -from pydantic import model_validator +import pandas as pd # noqa: TC002 +from pydantic import ConfigDict, model_validator from sift.runs.v2.runs_pb2 import CreateRunRequest as CreateRunRequestProto from sift.runs.v2.runs_pb2 import Run as RunProto @@ -40,6 +41,9 @@ class Run(BaseType[RunProto, "Run"], FileAttachmentsMixin): integers. """ + # pandas.Timestamp has no pydantic schema, so it is validated with isinstance. + model_config = ConfigDict(arbitrary_types_allowed=True) + # Required fields name: str description: str @@ -57,8 +61,8 @@ class Run(BaseType[RunProto, "Run"], FileAttachmentsMixin): is_archived: bool # Optional fields - start_time: datetime | None - stop_time: datetime | None + start_time: pd.Timestamp | datetime | None + stop_time: pd.Timestamp | datetime | None duration: timedelta | None default_report_id: str | None client_key: str | None @@ -75,14 +79,10 @@ def _from_proto(cls, proto: RunProto, sift_client: SiftClient | None = None) -> modified_by_user_id=proto.modified_by_user_id, organization_id=proto.organization_id, start_time=( - to_timestamp_nanos(proto.start_time).as_unit("ns") - if proto.HasField("start_time") - else None + to_timestamp_nanos(proto.start_time) if proto.HasField("start_time") else None ), stop_time=( - to_timestamp_nanos(proto.stop_time).as_unit("ns") - if proto.HasField("stop_time") - else None + to_timestamp_nanos(proto.stop_time) if proto.HasField("stop_time") else None ), duration=(proto.duration.ToTimedelta() if proto.HasField("duration") else None), name=proto.name, diff --git a/python/lib/sift_client/sift_types/test_report.py b/python/lib/sift_client/sift_types/test_report.py index b8b1f22364..9c21d37798 100644 --- a/python/lib/sift_client/sift_types/test_report.py +++ b/python/lib/sift_client/sift_types/test_report.py @@ -5,7 +5,8 @@ from enum import Enum from typing import TYPE_CHECKING, ClassVar -from pydantic import field_validator +import pandas as pd # noqa: TC002 +from pydantic import ConfigDict, field_validator from sift.test_reports.v1.test_reports_pb2 import ( CreateTestReportRequest as CreateTestReportRequestProto, ) @@ -28,6 +29,7 @@ TestStep as TestStepProto, ) +from sift_client._internal.time import to_timestamp_nanos from sift_client.sift_types._base import ( BaseType, MappingHelper, @@ -155,7 +157,14 @@ def to_proto(self) -> TestStepProto: class TestStep(BaseType[TestStepProto, "TestStep"], FileAttachmentsMixin, SimulatedMixin): - """TestStep model representing a step in a test.""" + """TestStep model representing a step in a test. + + ``start_time`` and ``end_time`` are nanosecond timestamps, the same + resolution as channel data from ``get_data``. + """ + + # pandas.Timestamp has no pydantic schema, so it is validated with isinstance. + model_config = ConfigDict(arbitrary_types_allowed=True) test_report_id: str parent_step_id: str | None = None @@ -164,8 +173,8 @@ class TestStep(BaseType[TestStepProto, "TestStep"], FileAttachmentsMixin, Simula step_type: TestStepType step_path: str status: TestStatus - start_time: datetime - end_time: datetime + start_time: pd.Timestamp | datetime + end_time: pd.Timestamp | datetime error_info: ErrorInfo | None = None # NOTE: update() replaces this map wholesale. See TODO(metadata-mixin) in # sift_types/_mixins/metadata.py before adding keys at runtime. @@ -187,8 +196,8 @@ def _from_proto(cls, proto: TestStepProto, sift_client: SiftClient | None = None step_type=TestStepType(proto.step_type), step_path=proto.step_path, status=TestStatus(proto.status), - start_time=proto.start_time.ToDatetime(tzinfo=timezone.utc), - end_time=proto.end_time.ToDatetime(tzinfo=timezone.utc), + start_time=to_timestamp_nanos(proto.start_time), + end_time=to_timestamp_nanos(proto.end_time), error_info=ErrorInfo._from_proto(proto.error_info, sift_client) if proto.HasField("error_info") else None, From 899d3e88e734cdd644c98df835bdaf4affc7efc8 Mon Sep 17 00:00:00 2001 From: Ian Later Date: Fri, 30 Oct 2026 12:42:16 -0700 Subject: [PATCH 6/6] python(fix): read channel index integers without DatetimeIndex.asi8 --- .../_tests/sift_types/channel_time.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/python/lib/sift_client/_tests/sift_types/channel_time.py b/python/lib/sift_client/_tests/sift_types/channel_time.py index b05b52e711..aab29d07b8 100644 --- a/python/lib/sift_client/_tests/sift_types/channel_time.py +++ b/python/lib/sift_client/_tests/sift_types/channel_time.py @@ -1,13 +1,20 @@ """Shared check that a time axis matches the index ``get_data`` builds.""" +from __future__ import annotations + +from typing import TYPE_CHECKING + +import numpy as np import pandas as pd from google.protobuf.any_pb2 import Any -from google.protobuf.timestamp_pb2 import Timestamp from sift.common.type.v1.channel_data_type_pb2 import CHANNEL_DATA_TYPE_DOUBLE from sift.data.v2.data_pb2 import DoubleValue, DoubleValues, Metadata from sift_client._internal.low_level_wrappers.data import DataLowLevelClient +if TYPE_CHECKING: + from google.protobuf.timestamp_pb2 import Timestamp + def channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: """Index ``get_data`` builds for double-channel samples at these times.""" @@ -30,10 +37,15 @@ def channel_index(*timestamps: Timestamp) -> pd.DatetimeIndex: return index +def _integer_view(index: pd.DatetimeIndex) -> list[int]: + """Integers in the index's own unit, the scale interpolation uses.""" + return [int(value) for value in np.asarray(index, dtype=np.int64)] + + def assert_axis_matches_channel(start, stop, *timestamps: Timestamp) -> None: """A ``date_range`` across these bounds uses the channel index scale.""" samples = channel_index(*timestamps) axis = pd.date_range(start, stop, periods=2) assert str(samples.dtype).startswith("datetime64[ns") assert axis.dtype == samples.dtype - assert [int(value) for value in axis.asi8] == [int(value) for value in samples.asi8] + assert _integer_view(axis) == _integer_view(samples)