Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions dataretrieval/ogc/shaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ def _arrange_cols(
df : pd.DataFrame
The input DataFrame whose columns are to be rearranged or renamed.
properties : Optional[List[str]]
A list of column names to possibly rename. If None or contains
only NaN, the function renames 'id' to output_id.
Requested column names. Camel-case names are matched to normalized
snake_case DataFrame columns. If None or contains only NaN, the
function renames 'id' to output_id.
output_id : str
The name to which the 'id' column should be renamed if applicable.
extra_id_cols : set or frozenset, optional
Expand Down Expand Up @@ -261,7 +262,10 @@ def _arrange_cols(

# --- Explicit properties: select and reorder columns per the list ---
# Do not alias the caller's list; it is mutated below.
local_properties = list(properties)
# Match the names normalized by _finalize_ogc before selecting columns.
local_properties = [
_to_snake_case(name) if isinstance(name, str) else name for name in properties
]
if "geometry" in df.columns and "geometry" not in local_properties:
local_properties.append("geometry")
# 'id' is a valid collection column, but expose it under the
Expand Down
25 changes: 25 additions & 0 deletions tests/waterdata_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ def test_finalize_ogc_truncates_combined_to_max_rows():
assert hasattr(md, "url") # wrapped as BaseMetadata


def test_finalize_ogc_keeps_requested_camel_case_property():
"""A selected API property remains in the normalized result frame."""
frame = pd.DataFrame(
{"id": ["obs-1"], "sampleTime": ["2024-01-01T00:00:00Z"], "value": [1.0]}
)
response = mock.MagicMock()
response.url = "https://example.com/collections/things/items"
response.elapsed = datetime.timedelta(seconds=0.1)
response.headers = {}
properties = ["sampleTime"]

result, _ = _finalize_ogc(
frame,
response,
properties=properties,
output_id="thing_id",
convert_type=False,
collection="things",
)

assert result.columns.tolist() == ["sample_time"]
assert result["sample_time"].tolist() == ["2024-01-01T00:00:00Z"]
assert properties == ["sampleTime"]


def _resp_ok(features):
"""Build a 200-OK mock response with the given features list."""
links = [{"rel": "next", "href": "https://example.com/page2"}] if features else []
Expand Down
Loading