polygon_query on points resets the index and rewrites the coordinate systems (adds a bogus `global: Identity`)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 394
- Forks
- 95
- Avg merge
- 4d 3h
- Merged PRs (30d)
- 7
Description
[!NOTE]
This whole message is AI-generated. The issue was automatically discovered and reported by an AI agent (Claude) during an autonomous bug hunt on thespatialdatacode base. It has not been verified or triaged by a human yet; theneeds: triagelabel is set so that a maintainer can confirm it. The reproduction script below was executed by the agent in an isolated environment (see Environment) and its output is pasted verbatim.
Summary
The dask → geopandas → dask round trip used by polygon_query calls reset_index(drop=True) and loses .attrs, so PointsModel.parse installs the default {"global": Identity()} and only the queried coordinate system is set on top. For points with index [100, 200, 300] and transformations {"aligned": Scale, "other": Identity} (no global), the result has index [0, 1] and transformations {"global": Identity, "aligned": Scale}: the original ids are gone, other is dropped and an invented mapping to global is added. bounding_box_query on the same element behaves correctly.
Severity (agent's assessment): high — point ids are lost and the returned element is mis-positioned in global
Where: src/spatialdata/_core/query/spatial_query.py, DaskDataFrame overload of polygon_query; src/spatialdata/models/_utils.py::points_dask_dataframe_to_geopandas (reset_index(drop=True))
Expected behaviour
Same behaviour as bounding_box_query: original index and all coordinate systems preserved, no global added.
Reproduction
Save as repro.py and run uv run repro.py (the PEP 723 header pins spatialdata to the commit the bug was found on; replace the URL fragment with @main to test the current main branch).
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "spatialdata @ git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0",
# ]
# ///
"""polygon_query on points resets the index and rewrites the coordinate systems (adds a bogus global Identity)."""
import warnings
import pandas as pd
from shapely.geometry import box
from spatialdata import bounding_box_query, polygon_query
from spatialdata.models import PointsModel
from spatialdata.transformations import Identity, Scale, get_transformation
warnings.simplefilter("ignore")
df = pd.DataFrame({"x": [1.0, 5.0, 9.0], "y": [1.0, 5.0, 9.0], "gene": pd.Categorical(["a", "b", "c"])}, index=[100, 200, 300])
points = PointsModel.parse(df, feature_key="gene", transformations={"aligned": Scale([2.0, 2.0], axes=("x", "y")), "other": Identity()})
print("original index:", df.index.tolist(), "| original coordinate systems:", sorted(get_transformation(points, get_all=True)))
poly = polygon_query(points, polygon=box(0, 0, 12, 12), target_coordinate_system="aligned")
bbox = bounding_box_query(points, axes=("x", "y"), min_coordinate=[0, 0], max_coordinate=[12, 12], target_coordinate_system="aligned")
print("polygon_query -> index:", poly.index.compute().tolist(), "| coordinate systems:", {k: type(v).__name__ for k, v in get_transformation(poly, get_all=True).items()})
print("bounding_box_query -> index:", bbox.index.compute().tolist(), "| coordinate systems:", sorted(get_transformation(bbox, get_all=True)))
print("expected for both: index [100, 200] and coordinate systems ['aligned', 'other'] (no 'global')")
bug = poly.index.compute().tolist() != [100, 200] or "global" in get_transformation(poly, get_all=True) or "other" not in get_transformation(poly, get_all=True)
print("VERDICT:", "BUG REPRODUCED" if bug else "NOT REPRODUCED")
Observed output
original index: [100, 200, 300] | original coordinate systems: ['aligned', 'other']
polygon_query -> index: [0, 1] | coordinate systems: {'global': 'Identity', 'aligned': 'Scale'}
bounding_box_query -> index: [100, 200] | coordinate systems: ['aligned', 'other']
expected for both: index [100, 200] and coordinate systems ['aligned', 'other'] (no 'global')
VERDICT: BUG REPRODUCED
Possible fix direction (unverified)
Filter the computed pandas frame directly (as bounding_box_query does) and re-parse with transformations=get_transformation(points, get_all=True).copy(); or make points_dask_dataframe_to_geopandas keep the index and attrs.
Environment
uv run repro.py with the PEP 723 metadata in the script (fresh, isolated environment; spatialdata built from main @ ccf1ea0 (2026-08-28); Python 3.13, latest releases of the dependencies at run time: pandas 3.0, anndata 0.13, zarr 3.3, dask 2026.8, numpy 2.5, geopandas 1.1, shapely 2.1). macOS (arm64). Also reproduced in a second environment with pandas 2.3.3 / anndata 0.12.11 / numpy 2.4.4 / zarr 3.2.1.
Automatically generated; discovered by an AI agent (Claude) and not yet reviewed by a human.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the DaskDataFrame overload of polygon_query in src/spatialdata/_core/query/spatial_query.py and points_dask_dataframe_to_geopandas in src/spatialdata/models/_utils.py; run the provided repro.py first. Compare polygon_query with bounding_box_query and verify that the queried points retain index [100, 200] and coordinate systems ['aligned', 'other'], without adding 'global'.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100