Skip to content

Commit 9175ff7

Browse files
authored
feat: write_dataset from pylist and pydict (#3527)
Fix this bug from `docs/read_and_write.rst` example: https://github.com/lancedb/lance/blob/e12bb9eff2a52f753668d4b62c52e4d72b10d294/docs/read_and_write.rst?plain=1#L264
1 parent e12bb9e commit 9175ff7

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

python/python/lance/types.py

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ def _coerce_reader(
7474
and data_obj.__class__.__name__ == "DataFrame"
7575
):
7676
return data_obj.to_arrow().to_reader()
77+
elif isinstance(data_obj, dict):
78+
batch = pa.RecordBatch.from_pydict(data_obj, schema=schema)
79+
return pa.RecordBatchReader.from_batches(batch.schema, [batch])
80+
elif (
81+
isinstance(data_obj, list)
82+
and len(data_obj) > 0
83+
and isinstance(data_obj[0], dict)
84+
):
85+
# List of dictionaries
86+
batch = pa.RecordBatch.from_pylist(data_obj, schema=schema)
87+
return pa.RecordBatchReader.from_batches(batch.schema, [batch])
7788
# for other iterables, assume they are of type Iterable[RecordBatch]
7889
elif isinstance(data_obj, Iterable):
7990
if schema is not None:

python/python/tests/test_dataset.py

+19
Original file line numberDiff line numberDiff line change
@@ -2992,3 +2992,22 @@ def test_empty_structs(tmp_path):
29922992
res = ds.take([2, 0, 1])
29932993
assert res.num_rows == 3
29942994
assert res == table.take([2, 0, 1])
2995+
2996+
2997+
def test_create_table_from_pylist(tmp_path):
2998+
data = [
2999+
{"foo": 1, "bar": "one"},
3000+
{"foo": 3, "bar": "three"},
3001+
]
3002+
ds = lance.write_dataset(data, tmp_path)
3003+
3004+
assert ds.to_table() == pa.Table.from_pylist(data)
3005+
3006+
3007+
def test_create_table_from_pydict(tmp_path):
3008+
dat = {
3009+
"foo": [1, 3],
3010+
"bar": ["one", "three"],
3011+
}
3012+
ds = lance.write_dataset(dat, tmp_path)
3013+
assert ds.to_table() == pa.Table.from_pydict(dat)

0 commit comments

Comments
 (0)