Skip to content

Commit

Permalink
Fix for both empty gataframes and object columns with all None values
Browse files Browse the repository at this point in the history
  • Loading branch information
theroggy committed Jan 26, 2025
1 parent ebb012f commit e04a8b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
14 changes: 9 additions & 5 deletions pyogrio/geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,6 @@ def write_dataframe(
use_arrow = bool(int(os.environ.get("PYOGRIO_USE_ARROW", "0")))
path, driver = _get_write_path_driver(path, driver, append=append)

if use_arrow and (df.empty or len(df) == 0):
# with arrow, string columns without data trigger an error, so disable arrow
# when writing an empty dataframe.
use_arrow = False

geometry_columns = df.columns[df.dtypes == "geometry"]
if len(geometry_columns) > 1:
raise ValueError(
Expand Down Expand Up @@ -588,6 +583,15 @@ def write_dataframe(

table = pa.Table.from_pandas(df, preserve_index=False)

# Null arrow columns are not supported by GDAL, so convert to string
for field_index, field in enumerate(table.schema):
if field.type == pa.null():
table = table.set_column(
field_index,
field.with_type(pa.string()),
table[field_index].cast(pa.string()),
)

if geometry_column is not None:
# ensure that the geometry column is binary (for all-null geometries,
# this could be a wrong type)
Expand Down
13 changes: 9 additions & 4 deletions pyogrio/tests/test_geopandas_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,11 @@ def test_write_dataframe_index(tmp_path, naturalearth_lowres, use_arrow):
)
@pytest.mark.requires_arrow_write_api
def test_write_empty_dataframe(tmp_path, ext, columns, dtype, use_arrow):
"""Test writing dataframe with no rows.
With use_arrow, object type columns with no rows are converted to null type columns
by pyarrow, but null columns are not supported by GDAL. Added to test fix for #513.
"""
expected = gp.GeoDataFrame(geometry=[], columns=columns, dtype=dtype, crs=4326)
filename = tmp_path / f"test{ext}"
write_dataframe(expected, filename, use_arrow=use_arrow)
Expand Down Expand Up @@ -1185,11 +1190,11 @@ def test_write_empty_geometry(tmp_path):

@pytest.mark.requires_arrow_write_api
def test_write_None_string_column(tmp_path, use_arrow):
if use_arrow:
pytest.xfail(
"error when an object column with only None values is written with arrow."
)
"""Test pandas object columns with all None values.
With use_arrow, such columns are converted to null type columns by pyarrow, but null
columns are not supported by GDAL. Added to test fix for #513.
"""
gdf = gp.GeoDataFrame({"object_col": [None]}, geometry=[Point(0, 0)], crs=4326)
filename = tmp_path / "test.gpkg"

Expand Down

0 comments on commit e04a8b1

Please sign in to comment.