Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Enable automatic type deduction for columns built from lists of dataframes. #386

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions torcharrow/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ def infer_dtype_from_value(value):
for t in value:
dtypes.append(infer_dtype_from_value(t))
return prt(value, Tuple(dtypes))

from torcharrow.velox_rt.dataframe_cpu import DataFrameCpu

if isinstance(value, DataFrameCpu):
return prt(value, List(value.dtype))

raise AssertionError(f"unexpected case {value} of type {type(value)}")


Expand Down Expand Up @@ -729,8 +735,14 @@ def common_dtype(l: DType, r: DType) -> ty.Optional[DType]:
if is_list(l) and is_list(r):
k = common_dtype(l.item_dtype, r.item_dtype)
return List(k).with_null(l.nullable or r.nullable) if k is not None else None
if is_struct(l) and is_struct(r):
if l.fields == r.fields:
return Struct(l.fields, l.nullable or r.nullable)
else:
return None
if l.with_null() == r.with_null():
return l if l.nullable else r

return None


Expand Down
13 changes: 13 additions & 0 deletions torcharrow/test/test_list_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ def base_test_fixed_size_list(self):
f"Unexpected failure reason: {str(ex.exception)}",
)

def base_test_column_from_dataframe_list(self):
a = ta.dataframe({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})
b = ta.column([a, a])
self.assertEqual(
list(b),
[[(1, 5), (2, 6), (3, 7), (4, 8)], [(1, 5), (2, 6), (3, 7), (4, 8)]],
)
self.assertEqual(
b.dtype,
dt.List(dt.Struct([dt.Field("a", dt.int64), dt.Field("b", dt.int64)])),
)
self.assertTrue(isinstance(b, ta.velox_rt.list_column_cpu.ListColumnCpu))


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions torcharrow/test/test_list_column_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def test_map_reduce_etc(self):
def test_fixed_size_list(self):
self.base_test_fixed_size_list()

def test_column_from_dataframe_list(self):
self.base_test_column_from_dataframe_list()


if __name__ == "__main__":
unittest.main()