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
33 changes: 29 additions & 4 deletions src/datasets/arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
BinaryIO,
Callable,
Optional,
Sequence,
Union,
overload,
)
Expand Down Expand Up @@ -1290,7 +1291,7 @@ def from_list(

@staticmethod
def from_csv(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
cache_dir: str = None,
Expand Down Expand Up @@ -1428,9 +1429,10 @@ def from_generator(
**kwargs,
).read()

@staticmethod
@staticmethod
def from_json(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
cache_dir: str = None,
Expand Down Expand Up @@ -1487,9 +1489,32 @@ def from_json(
**kwargs,
).read()

@staticmethod
def from_jsonl(
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
field: Optional[str] = None,
num_proc: Optional[int] = None,
**kwargs,
) -> "Dataset":
"""Alias of `Dataset.from_json`."""
return Dataset.from_json(
path_or_paths,
split=split,
features=features,
cache_dir=cache_dir,
keep_in_memory=keep_in_memory,
field=field,
num_proc=num_proc,
**kwargs,
)

@staticmethod
def from_parquet(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
cache_dir: str = None,
Expand Down Expand Up @@ -1586,7 +1611,7 @@ def from_parquet(

@staticmethod
def from_text(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
cache_dir: str = None,
Expand Down
26 changes: 22 additions & 4 deletions src/datasets/dataset_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ def load_from_disk(

@staticmethod
def from_csv(
path_or_paths: dict[str, PathLike],
path_or_paths: dict[str, Union[PathLike, Sequence[PathLike]]],
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -1485,9 +1485,10 @@ def from_csv(
**kwargs,
).read()

@staticmethod
@staticmethod
def from_json(
path_or_paths: dict[str, PathLike],
path_or_paths: dict[str, Union[PathLike, Sequence[PathLike]]],
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -1528,9 +1529,26 @@ def from_json(
**kwargs,
).read()

@staticmethod
def from_jsonl(
path_or_paths: dict[str, Union[PathLike, Sequence[PathLike]]],
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
**kwargs,
) -> "DatasetDict":
"""Alias of `DatasetDict.from_json`."""
return DatasetDict.from_json(
path_or_paths,
features=features,
cache_dir=cache_dir,
keep_in_memory=keep_in_memory,
**kwargs,
)

@staticmethod
def from_parquet(
path_or_paths: dict[str, PathLike],
path_or_paths: dict[str, Union[PathLike, Sequence[PathLike]]],
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -1579,7 +1597,7 @@ def from_parquet(

@staticmethod
def from_text(
path_or_paths: dict[str, PathLike],
path_or_paths: dict[str, Union[PathLike, Sequence[PathLike]]],
features: Optional[Features] = None,
cache_dir: str = None,
keep_in_memory: bool = False,
Expand Down
30 changes: 25 additions & 5 deletions src/datasets/iterable_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from functools import partial
from itertools import cycle, islice
from pathlib import Path
from typing import TYPE_CHECKING, Any, BinaryIO, Callable, Optional, Union
from typing import TYPE_CHECKING, Any, BinaryIO, Callable, Optional, Sequence, Union

import fsspec.asyn
import multiprocess as mp
Expand Down Expand Up @@ -3161,7 +3161,7 @@ def from_list(

@staticmethod
def from_csv(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -3202,9 +3202,10 @@ def from_csv(
**kwargs,
).read()

@staticmethod
@staticmethod
def from_json(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -3249,9 +3250,28 @@ def from_json(
**kwargs,
).read()

@staticmethod
def from_jsonl(
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
keep_in_memory: bool = False,
field: Optional[str] = None,
**kwargs,
) -> "IterableDataset":
"""Alias of `IterableDataset.from_json`."""
return IterableDataset.from_json(
path_or_paths,
split=split,
features=features,
keep_in_memory=keep_in_memory,
field=field,
**kwargs,
)

@staticmethod
def from_parquet(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
keep_in_memory: bool = False,
Expand Down Expand Up @@ -3336,7 +3356,7 @@ def from_parquet(

@staticmethod
def from_text(
path_or_paths: Union[PathLike, list[PathLike]],
path_or_paths: Union[PathLike, Sequence[PathLike]],
split: Optional[NamedSplit] = None,
features: Optional[Features] = None,
keep_in_memory: bool = False,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,15 @@ def _check_json_dataset(dataset, expected_features):
assert dataset.features[feature].dtype == expected_dtype


def test_dataset_from_jsonl(jsonl_path, tmp_path):
cache_dir = tmp_path / "cache"
expected_features = {"col_1": "string", "col_2": "int64", "col_3": "float64"}
dataset_json = Dataset.from_json(jsonl_path, cache_dir=cache_dir)
dataset_jsonl = Dataset.from_jsonl(jsonl_path, cache_dir=cache_dir)
assert dataset_json.data == dataset_jsonl.data
_check_json_dataset(dataset_jsonl, expected_features)


@pytest.mark.parametrize("keep_in_memory", [False, True])
def test_dataset_from_json_keep_in_memory(keep_in_memory, jsonl_path, tmp_path):
cache_dir = tmp_path / "cache"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_dataset_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,14 @@ def _check_json_datasetdict(dataset_dict, expected_features, splits=("train",)):
assert dataset.features[feature].dtype == expected_dtype


def test_datasetdict_from_jsonl(jsonl_path, tmp_path):
cache_dir = tmp_path / "cache"
expected_features = {"col_1": "string", "col_2": "int64", "col_3": "float64"}
dataset_json = DatasetDict.from_json({"train": jsonl_path}, cache_dir=cache_dir)
dataset_jsonl = DatasetDict.from_jsonl({"train": jsonl_path}, cache_dir=cache_dir)
assert dataset_json["train"].data == dataset_jsonl["train"].data
_check_json_datasetdict(dataset_jsonl, expected_features)

@pytest.mark.parametrize("keep_in_memory", [False, True])
def test_datasetdict_from_json_keep_in_memory(keep_in_memory, jsonl_path, tmp_path):
cache_dir = tmp_path / "cache"
Expand Down