scverse / scverse/spatialdata

A failing aggregate() leaves the internal __ones_column / __areas_column on the user's shapes element

Open
#1,231 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug 🚨 element: shapes ▲ method: aggregation 🔢 needs: triage
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 the spatialdata code base. It has not been verified or triaged by a human yet; the needs: triage label 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

After aggregate(values=shapes, by=other, value_key="v", agg_func="not_a_function") raises, shapes.columns contains __ones_column and __areas_column.

Severity (agent's assessment): low/medium — the next aggregate() on the same element fails with the reserved-columns assertion and write() persists the junk columns

Where: src/spatialdata/_core/operations/aggregate.py::_aggregate_shapes (columns added to values up front, cleanup only at the very end; to_polygons(GeoDataFrame) returns the same object for polygon inputs); same pattern for values_[ONES_KEY] in aggregate() and by[INDEX] when fractions=True

Expected behaviour

The input element is unchanged after an exception.

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",
# ]
# ///
"""A failing aggregate() leaves the internal __ones_column / __areas_column on the user's shapes element."""
import warnings
import geopandas as gpd
from shapely.geometry import box
from spatialdata import aggregate
from spatialdata.models import ShapesModel

warnings.simplefilter("ignore")
values = ShapesModel.parse(gpd.GeoDataFrame({"geometry": [box(0, 0, 1, 1), box(1, 1, 2, 2)], "v": [1.0, 2.0]}))
by = ShapesModel.parse(gpd.GeoDataFrame({"geometry": [box(0, 0, 2, 2)]}))
print("columns of `values` before:", list(values.columns))
try:
    aggregate(values=values, by=by, value_key="v", agg_func="not_a_function")
except Exception as e:  # noqa: BLE001
    print("aggregate raised", type(e).__name__, "(expected, invalid agg_func)")
print("columns of `values` after :", list(values.columns))
bug = "__ones_column" in values.columns
try:
    aggregate(values=values, by=by, value_key="v", agg_func="sum")
    print("second aggregate: OK")
except Exception as e:  # noqa: BLE001
    print("second aggregate:", type(e).__name__, str(e)[:90])
print("VERDICT:", "BUG REPRODUCED" if bug else "NOT REPRODUCED")
Observed output
columns of `values` before: ['geometry', 'v']
aggregate raised AttributeError (expected, invalid agg_func)
columns of `values` after : ['geometry', 'v', '__ones_column', '__areas_column']
second aggregate: OK
VERDICT: BUG REPRODUCED

Possible fix direction (unverified)

Work on a shallow copy (values = values.copy(), geometry is shared) or wrap the body in try/finally for the cleanup.

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/spatialdata/_core/operations/aggregate.py, especially _aggregate_shapes, and run the supplied repro.py with uv run. Check the failure paths for temporary __ones_column, __areas_column, and related columns, then verify that the input shapes element is unchanged after an exception and that a later aggregate() still succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.