|
1 | 1 | import uuid
|
2 | 2 | from packaging.version import Version
|
3 | 3 |
|
| 4 | +import dask |
4 | 5 | from dask import config
|
5 | 6 |
|
6 | 7 | # Check if dask-dataframe is using dask-expr (default of None means True as well)
|
@@ -84,3 +85,36 @@ def get_pyarrow_schema_geopandas(obj):
|
84 | 85 | for col in obj.columns[obj.dtypes == "geometry"]:
|
85 | 86 | df[col] = obj[col].to_wkb()
|
86 | 87 | return pa.Schema.from_pandas(df)
|
| 88 | + |
| 89 | + |
| 90 | +if Version(dask.__version__) >= Version("2023.6.1"): |
| 91 | + from dask.dataframe.dispatch import ( |
| 92 | + from_pyarrow_table_dispatch, |
| 93 | + to_pyarrow_table_dispatch, |
| 94 | + ) |
| 95 | + |
| 96 | + @to_pyarrow_table_dispatch.register((geopandas.GeoDataFrame,)) |
| 97 | + def get_pyarrow_table_from_geopandas(obj, **kwargs): |
| 98 | + # `kwargs` must be supported by `pyarrow.Table.from_pandas` |
| 99 | + import pyarrow as pa |
| 100 | + |
| 101 | + if Version(geopandas.__version__).major < 1: |
| 102 | + return pa.Table.from_pandas(obj.to_wkb(), **kwargs) |
| 103 | + else: |
| 104 | + # TODO handle kwargs? |
| 105 | + return pa.table(obj.to_arrow()) |
| 106 | + |
| 107 | + @from_pyarrow_table_dispatch.register((geopandas.GeoDataFrame,)) |
| 108 | + def get_geopandas_geodataframe_from_pyarrow(meta, table, **kwargs): |
| 109 | + # `kwargs` must be supported by `pyarrow.Table.to_pandas` |
| 110 | + if Version(geopandas.__version__).major < 1: |
| 111 | + df = table.to_pandas(**kwargs) |
| 112 | + |
| 113 | + for col in meta.columns[meta.dtypes == "geometry"]: |
| 114 | + df[col] = geopandas.GeoSeries.from_wkb(df[col], crs=meta[col].crs) |
| 115 | + |
| 116 | + return df |
| 117 | + |
| 118 | + else: |
| 119 | + # TODO handle kwargs? |
| 120 | + return geopandas.GeoDataFrame.from_arrow(table) |
0 commit comments