rok commented on code in PR #50325:
URL: https://github.com/apache/arrow/pull/50325#discussion_r3528401021


##########
python/pyarrow/types.pxi:
##########
@@ -1969,6 +1969,11 @@ cdef class JsonType(BaseExtensionType):
         return JsonScalar
 
 
+class _UuidPandasDtype:
+    def __from_arrow__(self, array):
+        return np.asarray(array.to_pylist(), dtype=object).reshape(1, -1)

Review Comment:
   Wouldn't this make `UuidType.__from_arrow__` return a 2-D array to satisfy 
old pandas `DataFrame` block construction, but the same hook is also used by 
`Array.to_pandas()`, which expects 1-D data for pd.Series?
   
   Perhaps a better fix is to keep `__from_arrow__` returning normal 1-D object 
data, and handle the old pandas block-manager reshape only in the `DataFrame` 
reconstruction path where 2-D block shape is
   actually needed?
   
   Something like:
   ```diff
   diff --git a/python/pyarrow/types.pxi b/python/pyarrow/types.pxi
   --- a/python/pyarrow/types.pxi
   +++ b/python/pyarrow/types.pxi
   @@ -1971,7 +1971,7 @@ cdef class JsonType(BaseExtensionType):
   
    class _UuidPandasDtype:
        def __from_arrow__(self, array):
   -        return np.asarray(array.to_pylist(), dtype=object).reshape(1, -1)
   +        # Return a 1-D object array of uuid.UUID values (nulls become None).
   +        # Per pandas' __from_arrow__ contract this is 1-D; the 
table-to-blocks
   +        # path reshapes it to the single-column block layout as needed.
   +        return np.asarray(array.to_pylist(), dtype=object)
   
   
    cdef class UuidType(BaseExtensionType):
   
   diff --git a/python/pyarrow/pandas_compat.py 
b/python/pyarrow/pandas_compat.py
   --- a/python/pyarrow/pandas_compat.py
   +++ b/python/pyarrow/pandas_compat.py
   @@ -779,6 +779,13 @@ def _reconstruct_block(item, columns=None, 
extension_columns=None,
            if not hasattr(pandas_dtype, '__from_arrow__'):
                raise ValueError("This column does not support to be converted "
                                 "to a pandas ExtensionArray")
            arr = pandas_dtype.__from_arrow__(arr)
   +        if isinstance(arr, np.ndarray) and arr.ndim == 1:
   +            # A plain (non-ExtensionArray) 1-D result — e.g. from the UUID
   +            # extension type — must be reshaped to the (1, n) single-column
   +            # block layout that make_block and create_dataframe_from_blocks
   +            # expect. pandas ExtensionArrays are 1-D and handled directly, 
so
   +            # they are intentionally left untouched here.
   +            arr = arr.reshape(1, -1)
        else:
            arr = block_arr
   ```



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