Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Preserve numeric column types in Tabular rows (#1298)

LimbC-C11b2cfe29847
diff --git a/src/python/txtai/pipeline/data/tabular.py b/src/python/txtai/pipeline/data/tabular.py
index 40ea1c2d..0cf5243d 100644
--- a/src/python/txtai/pipeline/data/tabular.py
+++ b/src/python/txtai/pipeline/data/tabular.py
@@ -103,7 +103,9 @@ class Tabular(Pipeline):
columns.remove(self.idcolumn)
# Transform into (id, text, tag) tuples
- for index, row in df.iterrows():
+ # Tuple iteration preserves column types instead of coercing each row to one dtype.
+ for index, values in zip(df.index, df.itertuples(index=False, name=None)):
+ row = dict(zip(df.columns, values))
uid = row[self.idcolumn] if self.idcolumn else index
uid = uid if uid is not None else index
text = self.concat(row, columns)
@@ -112,10 +114,10 @@ class Tabular(Pipeline):
# Also add row for content
if isinstance(self.content, list):
- row = {column: self.column(value) for column, value in row.to_dict().items() if column in self.content}
+ row = {column: self.column(value) for column, value in row.items() if column in self.content}
rows.append((uid, row, None))
elif self.content:
- row = {column: self.column(value) for column, value in row.to_dict().items()}
+ row = {column: self.column(value) for column, value in row.items()}
rows.append((uid, row, None))
return rows
diff --git a/test/python/testpipeline/testdata/testtabular.py b/test/python/testpipeline/testdata/testtabular.py
index d08eda2b..040dab56 100644
--- a/test/python/testpipeline/testdata/testtabular.py
+++ b/test/python/testpipeline/testdata/testtabular.py
@@ -53,6 +53,23 @@ class TestTabular(unittest.TestCase):
rows = tabular([{"id": 2, "quantity": "", "text": "Widget"}])
self.assertEqual(rows[0][1], "Widget")
+ def testNumericTypes(self):
+ """
+ Mixed numeric rows preserve integer IDs and content without float rounding.
+ """
+
+ data = [{"id": 2**53 + 1, "count": 2, "value": 1.5}, {"id": 2**53, "count": 3, "value": 2.5}]
+ for content in (False, True, ["id"]):
+ with self.subTest(content=content):
+ rows = Tabular("id", ["count", "value"], content)(data)
+ step = 2 if content else 1
+ self.assertTrue(all(isinstance(row[0], int) for row in rows))
+ self.assertEqual([row[0] for row in rows[::step]], [row["id"] for row in data])
+ self.assertEqual([row[1] for row in rows[::step]], ["2. 1.5", "3. 2.5"])
+ if content:
+ expected = data if content is True else [{"id": row["id"]} for row in data]
+ self.assertEqual([row[1] for row in rows[1::2]], expected)
+
def testContent(self):
"""
Test parsing additional content