Skip to content

Commit c2ac8c4

Browse files
committed
add example of adding new data
1 parent 557f44a commit c2ac8c4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/introduction/read_and_write.rst

+38
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,44 @@ You will need to provide a :py:class:`pyarrow.Schema` for the dataset in this ca
5050
:py:meth:`lance.write_dataset` supports writing :py:class:`pyarrow.Table`, :py:class:`pandas.DataFrame`,
5151
:py:class:`pyarrow.dataset.Dataset`, and ``Iterator[pyarrow.RecordBatch]``.
5252

53+
Adding data to an existing dataset
54+
----------------------------------
55+
56+
To insert data into your dataset, you can use either :py:meth:`LanceDataset.insert <lance.LanceDataset.insert>`
57+
or :py:meth:`~lance.write_dataset` with ``mode=append``.
58+
59+
.. testsetup::
60+
61+
shutil.rmtree("./insert_example.lance", ignore_errors=True)
62+
63+
.. doctest::
64+
65+
>>> import lance
66+
>>> import pyarrow as pa
67+
68+
>>> table = pa.Table.from_pylist([{"name": "Alice", "age": 20},
69+
... {"name": "Bob", "age": 30}])
70+
>>> ds = lance.write_dataset(table, "./insert_example.lance")
71+
72+
>>> new_table = pa.Table.from_pylist([{"name": "Carla", "age": 37}])
73+
>>> ds.insert(new_table)
74+
>>> tbl = lance.dataset("./insert_example.lance").to_table()
75+
>>> tbl.to_pandas()
76+
name age
77+
0 Alice 20
78+
1 Bob 30
79+
2 Carla 37
80+
81+
>>> new_table2 = pa.Table.from_pylist([{"name": "David", "age": 42}])
82+
>>> ds2 = lance.write_dataset(new_table2, "./insert_example.lance", mode="append")
83+
>>> ds2.to_table().to_pandas()
84+
name age
85+
0 Alice 20
86+
1 Bob 30
87+
2 Carla 37
88+
3 David 42
89+
90+
5391
Deleting rows
5492
-------------
5593

0 commit comments

Comments
 (0)