You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jorisvandenbossche (via GitHub)" <gi...@apache.org> on 2023/06/28 16:59:29 UTC

[GitHub] [arrow] jorisvandenbossche commented on a diff in pull request #36242: GH-34886: [Python] Add correct __array__ numpy conversion for Table and RecordBatch

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


##########
python/pyarrow/table.pxi:
##########
@@ -1487,6 +1487,16 @@ cdef class _Tabular(_PandasConvertible):
 
         return _PyArrowDataFrame(self, nan_as_null, allow_copy)
 
+    def __array__(self, dtype=None):
+        column_arrays = [
+            np.asarray(self.column(i), dtype=dtype) for i in range(self.num_columns)
+        ]
+        if column_arrays:
+            arr = np.stack(column_arrays, axis=1)
+        else:
+            arr = np.empty((self.num_rows, 0), dtype=dtype)

Review Comment:
   Yes, that is possible, you can have a table with a know number of rows but without columns:
   
   ```
   In [16]: table = pa.table({'a': [1, 2, 3]}).select([])
   
   In [17]: table
   Out[17]: 
   pyarrow.Table
   
   ----
   
   In [18]: table.num_rows
   Out[18]: 3
   
   In [19]: table.num_columns
   Out[19]: 0
   
   ```



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