jorisvandenbossche commented on a change in pull request #12311:
URL: https://github.com/apache/arrow/pull/12311#discussion_r800576144



##########
File path: python/pyarrow/table.pxi
##########
@@ -1779,15 +1796,41 @@ cdef class Table(_PandasConvertible):
         <pyarrow.lib.Table object at 0x7f05d1fb1b40>
         """
         from pyarrow.pandas_compat import dataframe_to_arrays
-        arrays, schema = dataframe_to_arrays(
+        arrays, schema, n_rows = dataframe_to_arrays(
             df,
             schema=schema,
             preserve_index=preserve_index,
             nthreads=nthreads,
             columns=columns,
             safe=safe
         )
-        return cls.from_arrays(arrays, schema=schema)
+
+        cdef:
+            vector[shared_ptr[CChunkedArray]] arr
+            shared_ptr[CSchema] c_schema
+        # If df is empty but row index is not, create empty Table with rows >0
+        if n_rows:
+            names = None
+            metadata = None
+            converted_arrays = _sanitize_arrays(arrays, names, schema, 
metadata,
+                                                &c_schema)
+            arr.reserve(0)
+            for item in converted_arrays:
+                if isinstance(item, Array):
+                    arr.push_back(
+                        make_shared[CChunkedArray](
+                            (<Array> item).sp_array
+                        )
+                    )
+                elif isinstance(item, ChunkedArray):
+                    arr.push_back((<ChunkedArray> item).sp_chunked_array)
+                else:
+                    raise TypeError(type(item))

Review comment:
       I think this can maybe be simplified? Because we now that `n_rows` is 
only defined if `arrays` is empty? In which case this for loop would not do 
anything? 
   And this also means that we don't need to call `_sanitize_arrays` I think. 
You can pass the existing schema (returned from `dataframe_to_arrays`) to 
`CTable.MakeWithRows` (to get the CSchema, you can unwrap it: `schema.unwrap()`)




-- 
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-unsubscr...@arrow.apache.org

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


Reply via email to