jorisvandenbossche commented on code in PR #38520:
URL: https://github.com/apache/arrow/pull/38520#discussion_r1423760032


##########
python/pyarrow/table.pxi:
##########
@@ -3986,6 +3986,60 @@ 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):
+            return 
Table.from_batches([RecordBatch.from_struct_array(struct_array)])
+        else:
+            return Table.from_batches([
+                RecordBatch.from_struct_array(chunk)
+                for chunk in struct_array.chunks
+            ])
+
+    def to_struct_array(self, max_chunksize=None):
+        """
+        Convert to a struct array.

Review Comment:
   Maybe we can be more explicit that it will be a chunked array of struct type?



##########
python/pyarrow/tests/test_table.py:
##########
@@ -878,6 +879,87 @@ def test_recordbatch_from_struct_array():
     ))
 
 
+def test_recordbatch_to_struct_array():
+    batch = pa.RecordBatch.from_arrays(
+        [
+            pa.array([1, None], type=pa.int32()),
+            pa.array([None, 1.0], type=pa.float32()),
+        ], ["ints", "floats"]
+    )
+    result = batch.to_struct_array()
+    assert result.equals(pa.array(
+        [{"ints": 1}, {"floats": 1.0}],
+        type=pa.struct([("ints", pa.int32()), ("floats", pa.float32())]),
+    ))
+
+
+def test_table_from_struct_array_invalid():
+    with pytest.raises(
+        TypeError,
+        match=re.escape(
+            "Argument 'struct_array' has incorrect type "
+            "(expected pyarrow.lib.StructArray, got pyarrow.lib.Int64Array)",
+        ),
+    ):

Review Comment:
   ```suggestion
       with pytest.raises(TypeError, match="Argument 'struct_array' has 
incorrect type"):
   ```
   
   This is a bit simpler, and more than good enough I think (the actual content 
of the error message is generated by cython)



-- 
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]

Reply via email to