ARROW-5178: [Python] Add Table.from_pydict()

This commit is contained in:
Antoine Pitrou committed 2019-04-18 11:31:40 +02:00
1 parent 0e99fc4622
commit 7b8fad5a3f
3 files changed
+89 -2

No files matched your search

-1
View File
@@ -23,7 +23,6 @@ from collections import OrderedDict
import datetime
import decimal as _pydecimal
import json
import multiprocessing
import numpy as np
import os
import six
+37 -1
View File
@@ -1160,6 +1160,8 @@ cdef class Table(_PandasConvertible):
inferred. If Arrays passed, this argument is required
schema : Schema, default None
If not passed, will be inferred from the arrays
metadata : dict or Mapping, default None
Optional metadata for the schema (if inferred).
Returns
-------
@@ -1176,7 +1178,9 @@ cdef class Table(_PandasConvertible):
_schema_from_arrays(arrays, names, metadata, &c_schema)
elif schema is not None:
if names is not None:
raise ValueError('Cannot pass schema and arrays')
raise ValueError('Cannot pass both schema and names')
if metadata is not None:
raise ValueError('Cannot pass both schema and metadata')
cy_schema = schema
if len(schema) != len(arrays):
@@ -1214,6 +1218,38 @@ cdef class Table(_PandasConvertible):
return pyarrow_wrap_table(CTable.Make(c_schema, columns))
@staticmethod
def from_pydict(mapping, schema=None, metadata=None):
"""
Construct a Table from Arrow arrays or columns
Parameters
----------
mapping : dict or Mapping
A mapping of strings to Arrays or Python lists.
schema : Schema, default None
If not passed, will be inferred from the Mapping values
metadata : dict or Mapping, default None
Optional metadata for the schema (if inferred).
Returns
-------
pyarrow.Table
"""
names = []
arrays = []
for k, v in mapping.items():
names.append(k)
if not isinstance(v, (Array, ChunkedArray)):
v = array(v)
arrays.append(v)
if schema is None:
return Table.from_arrays(arrays, names, metadata=metadata)
else:
# Will raise if metadata is not None
return Table.from_arrays(arrays, schema=schema, metadata=metadata)
@staticmethod
def from_batches(batches, Schema schema=None):
"""
+52
View File
@@ -906,3 +906,55 @@ def test_invalid_table_construct():
with pytest.raises(pa.lib.ArrowInvalid):
pa.Table.from_arrays(arrays, names=["a1", "a2"])
def test_table_from_pydict():
table = pa.Table.from_pydict({})
assert table.num_columns == 0
assert table.num_rows == 0
assert table.schema == pa.schema([])
assert table.to_pydict() == {}
# With arrays as values
data = OrderedDict([('strs', pa.array([u'', u'foo', u'bar'])),
('floats', pa.array([4.5, 5, None]))])
schema = pa.schema([('strs', pa.utf8()), ('floats', pa.float64())])
table = pa.Table.from_pydict(data)
assert table.num_columns == 2
assert table.num_rows == 3
assert table.schema == schema
# With chunked arrays as values
data = OrderedDict([('strs', pa.chunked_array([[u''], [u'foo', u'bar']])),
('floats', pa.chunked_array([[4.5], [5, None]]))])
table = pa.Table.from_pydict(data)
assert table.num_columns == 2
assert table.num_rows == 3
assert table.schema == schema
# With lists as values
data = OrderedDict([('strs', [u'', u'foo', u'bar']),
('floats', [4.5, 5, None])])
table = pa.Table.from_pydict(data)
assert table.num_columns == 2
assert table.num_rows == 3
assert table.schema == schema
assert table.to_pydict() == data
# With metadata and inferred schema
metadata = {b'foo': b'bar'}
schema = schema.add_metadata(metadata)
table = pa.Table.from_pydict(data, metadata=metadata)
assert table.schema == schema
assert table.schema.metadata == metadata
assert table.to_pydict() == data
# With explicit schema
table = pa.Table.from_pydict(data, schema=schema)
assert table.schema == schema
assert table.schema.metadata == metadata
assert table.to_pydict() == data
# Cannot pass both schema and metadata
with pytest.raises(ValueError):
pa.Table.from_pydict(data, schema=schema, metadata=metadata)