amol- commented on a change in pull request #12010:
URL: https://github.com/apache/arrow/pull/12010#discussion_r778812702
##########
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:
Right, my misunderstanding was that we could end up with `v=None`, but
rereading the code block I see that we would actually end up with `v=[None,
None, None]` which is fine.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]