Skip to content

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

Open

Description

@LucaMarconato

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions