You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/01/05 10:09:12 UTC

[GitHub] [arrow] AlenkaF commented on a change in pull request #12010: ARROW-6001 [Python]: Add from_pylist() and to_pylist() to pyarrow.Table to convert list of records

AlenkaF commented on a change in pull request #12010:
URL: https://github.com/apache/arrow/pull/12010#discussion_r778695741



##########
File path: python/pyarrow/table.pxi
##########
@@ -2442,6 +2602,46 @@ def _from_pydict(cls, mapping, schema, metadata):
         raise TypeError('Schema must be an instance of pyarrow.Schema')
 
 
+def _from_pylist(cls, mapping, schema, metadata):
+    """
+    Construct a Table/RecordBatch from list of dictionary of rows.
+
+    Parameters
+    ----------
+    cls : Class Table/RecordBatch
+    mapping : list of dicts of rows
+        A mapping of strings to row values.
+    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
+    -------
+    Table/RecordBatch
+    """
+
+    arrays = []
+    if schema is None:
+        names = []
+        if mapping:
+            names = list(mapping[0].keys())
+        for n in names:
+            v = [i[n] if n in i else None for i in mapping]
+            arrays.append(asarray(v))
+        return cls.from_arrays(arrays, names, metadata=metadata)
+    else:
+        if isinstance(schema, Schema):
+            for n in schema.names:
+                v = [i[n] if n in i else None for i in mapping]
+                n_type = schema.types[schema.get_field_index(n)]
+                arrays.append(asarray(v, type=n_type))

Review comment:
       The test for the case where schema names are missing from the pylist is added in this PR and it passes:
   https://github.com/apache/arrow/blob/19f212f4f7af0f7720d79a927c4850849da77678/python/pyarrow/tests/test_table.py#L1525-L1538




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org