Pass query inputs and results between Python libraries#
Register a producer’s table to query it with Geometry, then choose a result export that the receiving library can represent. The copy choice determines how long the producer must remain unchanged.
Register the producer’s table#
Session.table_from_arrow tries the Arrow device array, device stream, host array and host stream interfaces in that order. Host batches are uploaded. With copy=False, an eligible single CUDA batch is borrowed: keep its producer alive and do not mutate its storage until the final consumer releases it. Registration waits on the batch’s synchronization event. Use copy=True when the query needs independent storage.
For a cuDF producer, call session.table_from_cudf(frame, copy=False) to borrow immutable producer buffers through the final consumer, or pass copy=True to snapshot them. The adapter orders streams and retains exporting objects. If prior writes use an explicit producer stream, pass it as producer_stream and keep it valid. A result passed back into its session can use retained storage.
Check a column in the receiving dataframe#
This scalar projection can be represented by pandas and cuDF. It registers a host Arrow table, queries it and inspects the pandas column.
import pyarrow as pa
from cugeo.geometry import Session
session = Session()
table = pa.table({'district_id': pa.array([7], type=pa.int64())})
districts = session.table_from_arrow(table)
result = session.sql('SELECT district_id FROM districts', tables={'districts': districts})
frame = result.to_pandas()
print(frame['district_id'].iloc[0])
print(frame['district_id'].dtype)
The value is 7, and the column has an Arrow-backed 64-bit integer dtype.
Choose an export#
Interface |
Accepted output and destination |
|---|---|
|
Host Python tuples. Geometry and bytea are bytes. |
|
Independently owned host PyArrow table. |
|
Host pandas frame with ArrowDtype columns and field metadata. Labels must be unique. |
|
Host GeoDataFrame for qualified planar XY typed Geometry outputs. Labels must be unique. Infer the sole geometry or select its exact name. Result conversion requires geometry outputs with no CRS. |
|
GPU cuDF frame through Arrow Device. Labels must be unique. Geometry, bytea and arbitrary-precision numeric outputs are refused. |
For GeoPandas, the adapter supports WKB/EWKB for the seven canonical geometry families and a narrower native Arrow layout for Point, Polygon and MultiLineString. Its CRS checks admit absent, unknown or qualified uniform CRS where applicable and refuse opaque or rowwise CRS conditions. Result.to_geopandas has the stricter no-CRS condition above. The qualified consumer versions are GeoPandas 1.1.4, Shapely 2.1.2, PyArrow 25.0.1 and GEOS 3.13.1/CAPI 1.19.2. Result conversion creates a fresh RangeIndex. Eager-frame conversion has a separate index-preserving contract. See the GeoPandas adapter for its exact checks.
If cuDF needs only identifiers, project scalar IDs with distinct names. If the consumer needs shapes, choose a geometry-preserving export that admits their layout. Finish reads on any nonblocking CUDA stream before dropping a cuDF frame returned by to_cudf().
Hand a result to an Arrow Device consumer#
An unbound __arrow_c_device_array__ export returns fresh schema and device-array capsules. The consumer must wait on the array’s sync_event before reading and finish every read before releasing the array. requested_schema does not cast the result.
For result.export(stream=stream), supply a live integer CUDA stream handle or __cuda_stream__ provider on the result’s device. The stream is ordered after the result before export returns. Releasing the array synchronizes that stream. Keep the stream valid through release. The bound export hands over its capsules once. See device buffers, streams and ownership for the ordering and release rules.