fix(ogc): keep the frame type when a result is empty - #373
Draft
thodson-usgs wants to merge 3 commits into
Draft
Conversation
thodson-usgs
force-pushed
the
fix/empty-result-frame-type
branch
from
August 13, 2026 01:36
ca1cf51 to
efffe5f
Compare
Every OGC getter returned a ``GeoDataFrame`` when its filter matched at least one row and a plain ``DataFrame`` when it matched none. ``_empty_feature_frame`` exists to guarantee an empty page keeps the concrete class -- its docstring calls itself "the single home for this empty-page contract", because a later ``pd.concat([empty, geo_page])`` would otherwise strip geometry and CRS. But ``_deal_with_empty`` runs last, on every getter, 160 lines away in the same file, and built a fresh ``pd.DataFrame(columns=...)`` -- discarding the guarantee it was handed. So ``.geometry``, ``.to_crs()``, ``.plot()`` and ``pd.concat`` across a loop of per-site calls broke only on the empty result: the case least likely to be covered by a caller's tests, and invisible from ``df.columns`` because the ``geometry`` column is still listed. Reindexing the frame we were given applies the schema's columns while keeping the class -- and the ``geometry`` dtype with it. The plain-pandas path is unchanged: ``pd.DataFrame().reindex(columns=[...])`` is still a ``pd.DataFrame``. The regression test uses a ``DataFrame`` subclass rather than real geopandas, so the invariant is checked even where geopandas is absent (it is not in the ``[test]`` extra). Verified to fail against the old line and pass against the new one. 788 passed, mypy --strict clean, all hooks pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BTaSm7HmVb94RSJiKW4WAS
thodson-usgs
force-pushed
the
fix/empty-result-frame-type
branch
from
August 13, 2026 01:52
efffe5f to
d18b0c2
Compare
This was referenced Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Every OGC getter returns a
GeoDataFramewhen its filter matches at least one row, and a plainDataFramewhen it matches none.So
.geometry,.to_crs(),.plot(), andpd.concatacross a loop of per-site calls break only on the empty result — the case least likely to be covered by a caller's tests. It is also invisible fromdf.columns, because thegeometrycolumn is still listed.Why
ogc/shaping.py::_empty_feature_frameexists precisely to prevent this; its docstring calls itself "the single home for this empty-page contract", because a laterpd.concat([empty_page, geo_page])would otherwise downgrade the result and strip geometry/CRS.But
_deal_with_emptyruns last, on every getter, 160 lines away in the same file — and built a freshpd.DataFrame(columns=properties), discarding the guarantee it had just been handed.The fix
Reindex the frame we were given instead of constructing a new one. That applies the schema's columns while keeping the concrete class — and the
geometrydtype with it:The plain-pandas path is unchanged:
pd.DataFrame().reindex(columns=[...])is still apd.DataFrame.Testing
DataFramesubclass rather than real geopandas, so the invariant is checked even where geopandas is absent (it is not in the[test]extra, only[nldi]and[doc]). Verified to fail against the old line and pass against the new one.mypy --strict,ruff,xenon,complexipy,lint-importsall pass.Found by the scan in #372, split out because it changes user-visible behavior. Independent of #372 — branches off main.
🤖 Generated with Claude Code
https://claude.ai/code/session_01BTaSm7HmVb94RSJiKW4WAS