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?
##########
python/pyarrow/tests/parquet/test_data_types.py:
##########
@@ -604,6 +604,27 @@ def test_uuid_extension_type():
store_schema=False)
[email protected]
+def test_uuid_roundtrip(tempdir):
+ import uuid
+ u1, u2 = uuid.uuid4(), uuid.uuid4()
+ df = pd.DataFrame({"id": [u1, None, u2]})
+ table = pa.Table.from_pandas(df)
+ assert table.column("id").type == pa.uuid()
+
+ path = tempdir / "uuid_pandas_roundtrip.parquet"
+ pq.write_table(table, path)
+ read_table = pq.read_table(path)
+ assert read_table.column("id").type == pa.uuid()
+
+ result_df = read_table.to_pandas()
+ assert isinstance(result_df.loc[0, "id"], uuid.UUID)
+ assert isinstance(result_df.loc[2, "id"], uuid.UUID)
+ assert result_df.loc[0, "id"] == u1
+ assert result_df.loc[2, "id"] == u2
+ assert pd.isna(result_df.loc[1, "id"])
+
+
Review Comment:
Would this test pass with the current proposal?
```suggestion
@pytest.mark.pandas
def test_uuid_array_to_pandas():
from uuid import uuid4
import pandas as pd
import pandas.testing as tm
values = [uuid4(), None, uuid4()]
arr = pa.array(values, type=pa.uuid())
result = arr.to_pandas()
expected = pd.Series(values, dtype=object)
tm.assert_series_equal(result, expected)
```
--
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]