From 752fb97ea3afda34232fa34be28b08629bb7867c Mon Sep 17 00:00:00 2001
From: David Hoese <david.hoese@ssec.wisc.edu>
Date: Thu, 16 Mar 2023 21:10:29 -0500
Subject: [PATCH] Fix handling of missing influxdb columns

---
 metobsapi/tests/test_data_api.py | 15 +++++++++++++++
 metobsapi/util/query_influx.py   |  2 +-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/metobsapi/tests/test_data_api.py b/metobsapi/tests/test_data_api.py
index 272f993..78ec1a4 100644
--- a/metobsapi/tests/test_data_api.py
+++ b/metobsapi/tests/test_data_api.py
@@ -337,6 +337,21 @@ def test_shorthand_one_symbol_json_column(client):
     assert len(res["results"]["timestamps"]) == 9
 
 
+@pytest.mark.usefixtures("influxdb_air_temp_9_values")
+def test_shorthand_fill_symbol_json_column(client):
+    """Test that a valid column that is not available is still present in the result."""
+    res = client.get("/api/data.json?site=aoss&inst=tower&symbols=air_temp:wind_speed&begin=-00:10:00&order=column")
+    res = json.loads(res.data.decode())
+    assert res["code"] == 200
+    assert res["num_results"] == 9
+    assert "air_temp" in res["results"]["data"]
+    assert "wind_speed" in res["results"]["data"]
+    assert len(res["results"]["data"]["air_temp"]) == 9
+    assert len(res["results"]["data"]["wind_speed"]) == 9
+    assert np.isnan(res["results"]["data"]["wind_speed"]).all()
+    assert len(res["results"]["timestamps"]) == 9
+
+
 @pytest.mark.usefixtures("influxdb_3_symbols_9_values")
 def test_shorthand_two_symbols_json_column_order_check(client):
     res1 = _query_with_symbols(client, "air_temp:rel_hum:wind_speed")
diff --git a/metobsapi/util/query_influx.py b/metobsapi/util/query_influx.py
index 15d8f78..49a5468 100644
--- a/metobsapi/util/query_influx.py
+++ b/metobsapi/util/query_influx.py
@@ -90,7 +90,7 @@ class QueryHandler:
                 data_frame = frames_for_inst[0]
                 data_frame = data_frame.drop(columns=["site", "inst"])
                 # get dataframe into the same order that was requested
-                data_frame = data_frame[symbol_names]
+                data_frame = data_frame.reindex(columns=symbol_names)
             # "_time" should already be set as the index, so we don't need to rename it
             new_column_names = [f"{site}.{inst}.{col_name}" for col_name in data_frame.columns]
             data_frame.columns = new_column_names
-- 
GitLab