Commit 9175ff7 1 parent e12bb9e commit 9175ff7 Copy full SHA for 9175ff7
File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,17 @@ def _coerce_reader(
74
74
and data_obj .__class__ .__name__ == "DataFrame"
75
75
):
76
76
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 ])
77
88
# for other iterables, assume they are of type Iterable[RecordBatch]
78
89
elif isinstance (data_obj , Iterable ):
79
90
if schema is not None :
Original file line number Diff line number Diff line change @@ -2992,3 +2992,22 @@ def test_empty_structs(tmp_path):
2992
2992
res = ds .take ([2 , 0 , 1 ])
2993
2993
assert res .num_rows == 3
2994
2994
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 )
You can’t perform that action at this time.
0 commit comments