Elffuss BraidHub

kikocis/lancedb public

feat(python): drop support for dictionary in Table.add (#1725)

PR closes #1706
Changesetfb6326207146bcccdd24daceb1fd67a111fb595bf46f01e4a64331343f9e7c50
AuthorGagan Bhullar <g.deepsingh1@gmail.com> · 2024-10-08 20:41 -06:00
CommitterGitHub <noreply@github.com> · 2024-10-08 20:41 -06:00
Parents60fbe9d6df68

Browse the files at this changeset

4 files changed

modified python/python/lancedb/common.py +1 −1
@@ -20,7 +20,7 @@
2020
2121 pd = safe_import_pandas()
2222
23-DATA = Union[List[dict], dict, "pd.DataFrame", pa.Table, Iterable[pa.RecordBatch]]
23+DATA = Union[List[dict], "pd.DataFrame", pa.Table, Iterable[pa.RecordBatch]]
2424 VEC = Union[list, np.ndarray, pa.Array, pa.ChunkedArray]
2525 URI = Union[str, Path]
2626 VECTOR_COLUMN_NAME = "vector"
modified python/python/lancedb/db.py +2 −2
@@ -96,7 +96,7 @@
9696 User must provide at least one of `data` or `schema`.
9797 Acceptable types are:
9898
99- - dict or list-of-dict
99+ - list-of-dict
100100
101101 - pandas.DataFrame
102102
@@ -579,7 +579,7 @@
579579 User must provide at least one of `data` or `schema`.
580580 Acceptable types are:
581581
582- - dict or list-of-dict
582+ - list-of-dict
583583
584584 - pandas.DataFrame
585585
modified python/python/lancedb/table.py +6 −6
@@ -31,7 +31,6 @@
3131 import pyarrow.fs as pa_fs
3232 from lance import LanceDataset
3333 from lance.dependencies import _check_for_hugging_face
34-from lance.vector import vec_to_table
3534
3635 from .common import DATA, VEC, VECTOR_COLUMN_NAME
3736 from .embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry
@@ -87,6 +86,9 @@
8786 if isinstance(data, LanceModel):
8887 raise ValueError("Cannot add a single LanceModel to a table. Use a list.")
8988
89+ if isinstance(data, dict):
90+ raise ValueError("Cannot add a single dictionary to a table. Use a list.")
91+
9092 if isinstance(data, list):
9193 # convert to list of dict if data is a bunch of LanceModels
9294 if isinstance(data[0], LanceModel):
@@ -98,8 +100,6 @@
98100 return pa.Table.from_batches(data, schema=schema)
99101 else:
100102 return pa.Table.from_pylist(data)
101- elif isinstance(data, dict):
102- return vec_to_table(data)
103103 elif _check_for_pandas(data) and isinstance(data, pd.DataFrame):
104104 # Do not add schema here, since schema may contains the vector column
105105 table = pa.Table.from_pandas(data, preserve_index=False)
@@ -554,7 +554,7 @@
554554 data: DATA
555555 The data to insert into the table. Acceptable types are:
556556
557- - dict or list-of-dict
557+ - list-of-dict
558558
559559 - pandas.DataFrame
560560
@@ -1409,7 +1409,7 @@
14091409
14101410 Parameters
14111411 ----------
1412- data: list-of-dict, dict, pd.DataFrame
1412+ data: list-of-dict, pd.DataFrame
14131413 The data to insert into the table.
14141414 mode: str
14151415 The mode to use when writing the data. Valid values are
@@ -2348,7 +2348,7 @@
23482348 data: DATA
23492349 The data to insert into the table. Acceptable types are:
23502350
2351- - dict or list-of-dict
2351+ - list-of-dict
23522352
23532353 - pandas.DataFrame
23542354
modified python/python/tests/test_table.py +18 −0
@@ -193,6 +193,24 @@
193193 tbl.add(data=data)
194194
195195
196+def test_add_dictionary(db):
197+ schema = pa.schema(
198+ [
199+ pa.field("vector", pa.list_(pa.float32(), 2)),
200+ pa.field("item", pa.string()),
201+ pa.field("price", pa.float32()),
202+ ]
203+ )
204+ tbl = LanceTable.create(db, "test", schema=schema)
205+ data = {"vector": [3.1, 4.1], "item": "foo", "price": 10.0}
206+ with pytest.raises(ValueError) as excep_info:
207+ tbl.add(data=data)
208+ assert (
209+ str(excep_info.value)
210+ == "Cannot add a single dictionary to a table. Use a list."
211+ )
212+
213+
196214 def test_add(db):
197215 schema = pa.schema(
198216 [