Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/datasets/packaged_modules/tsfile/tsfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@
# Type helpers
# ---------------------------------------------------------------------------

def _require_tsfile():
try:
import tsfile # noqa: F401
except ImportError as e:
raise ImportError("To support loading TSFile datasets, please install 'tsfile'.") from e


def _arrow_type(ts_dtype, *, unit: str, tz: Optional[str]) -> pa.DataType:
"""Map a tsfile ``TSDataType`` to its Arrow representation."""
_require_tsfile()
from tsfile.constants import TSDataType

return {
Expand Down Expand Up @@ -86,6 +93,7 @@ def _promote_tsdatatype(a, b):
if a == b:
return a

_require_tsfile()
from tsfile.constants import TSDataType

table = {
Expand Down Expand Up @@ -301,6 +309,7 @@ def _generate_tables(self, files):

def _scan_metadata(self, files) -> Optional[dict]:
"""Walk every file and unify table name, TAG columns, FIELD types."""
_require_tsfile()
from tsfile.constants import TIME_COLUMN, ColumnCategory

wanted_table = self._table
Expand Down Expand Up @@ -431,6 +440,7 @@ def _build_device_index(self, readers: dict):
- ``file_meta``: maps each readable file to its per-file context
(``tag_cols``, ``field_cols``, ``time_col``).
"""
_require_tsfile()
from tsfile.constants import ColumnCategory

device_to_files: dict[tuple, list[str]] = {}
Expand Down Expand Up @@ -541,6 +551,7 @@ def _read_device_from_file(self, reader, meta: dict, device_key: tuple) -> tuple
field columns that this file owns *and* that the builder requested;
callers fill missing fields with all-null contributions.
"""
_require_tsfile()
from tsfile import tag_eq

file_tag_cols: list[str] = meta["tag_cols"]
Expand Down Expand Up @@ -730,6 +741,7 @@ def _open_reader(file: str):
segfaults. The 6-byte ``TsFile`` magic header is checked first to
bail out cleanly.
"""
_require_tsfile()
from tsfile import TsFileReader

try:
Expand Down
11 changes: 11 additions & 0 deletions tests/packaged_modules/test_tsfile_missing_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from datasets import load_dataset


def test_tsfile_missing_dependency_message(tmp_path):
tsfile_path = tmp_path / "sample.tsfile"
tsfile_path.write_bytes(b"fake tsfile content")

with pytest.raises(ImportError, match=r"To support loading TSFile datasets, please install 'tsfile'\."):
load_dataset("tsfile", data_files=str(tsfile_path))