You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "judahrand (via GitHub)" <gi...@apache.org> on 2023/12/11 16:00:49 UTC
Re: [PR] GH-33500: [Python] add `Table.to/from_struct_array` [arrow]
judahrand commented on code in PR #38520:
URL: https://github.com/apache/arrow/pull/38520#discussion_r1422716152
##########
python/pyarrow/table.pxi:
##########
@@ -3986,6 +3986,59 @@ cdef class Table(_Tabular):
result.validate()
return result
+ @staticmethod
+ def from_struct_array(struct_array):
+ """
+ Construct a Table from a StructArray.
+
+ Each field in the StructArray will become a column in the resulting
+ ``Table``.
+
+ Parameters
+ ----------
+ struct_array : StructArray or ChunkedArray
+ Array to construct the table from.
+
+ Returns
+ -------
+ pyarrow.Table
+
+ Examples
+ --------
+ >>> import pyarrow as pa
+ >>> struct = pa.array([{'n_legs': 2, 'animals': 'Parrot'},
+ ... {'year': 2022, 'n_legs': 4}])
+ >>> pa.Table.from_struct_array(struct).to_pandas()
+ animals n_legs year
+ 0 Parrot 2 NaN
+ 1 None 4 2022.0
+ """
+ if isinstance(struct_array, Array):
+ struct_array = chunked_array([struct_array])
+ return Table.from_batches([
+ RecordBatch.from_struct_array(chunk)
+ for chunk in struct_array.chunks
+ ])
Review Comment:
https://github.com/apache/arrow/pull/38520/commits/52a792a6d6b371322c3bc860301ecb21467afbe0
--
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