You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/10/13 16:26:19 UTC

(superset) branch master updated: fix(post-processing): keep the caller's index when parsing geography columns (#45086)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cd13e92c1d fix(post-processing): keep the caller's index when parsing geography columns (#45086)
0cd13e92c1d is described below

commit 0cd13e92c1df856b39f5237de1090850d8b69ea9
Author: Sepuri Sai Krishna <sa...@gmail.com>
AuthorDate: Tue Oct 13 21:56:12 2026 +0530

    fix(post-processing): keep the caller's index when parsing geography columns (#45086)
---
 superset/utils/pandas_postprocessing/geography.py  |  8 ++-
 .../pandas_postprocessing/test_geography.py        | 81 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 2 deletions(-)

diff --git a/superset/utils/pandas_postprocessing/geography.py b/superset/utils/pandas_postprocessing/geography.py
index cded3cbb82b..4f4eedee73d 100644
--- a/superset/utils/pandas_postprocessing/geography.py
+++ b/superset/utils/pandas_postprocessing/geography.py
@@ -38,7 +38,10 @@ def geohash_decode(
     :return: DataFrame with decoded longitudes and latitudes
     """
     try:
-        lonlat_df = DataFrame()
+        # Carry `df`'s own index: `_append_columns` aligns on it, so a parsed
+        # frame left on a fresh `RangeIndex` matches nothing when the caller's
+        # frame is indexed by anything else, as it is after a `pivot`.
+        lonlat_df = DataFrame(index=df.index)
         lonlat_df["latitude"], lonlat_df["longitude"] = zip(
             *df[geohash].apply(geohash_lib.decode), strict=False
         )
@@ -106,7 +109,8 @@ def geodetic_parse(
         return point[0], point[1], point[2]
 
     try:
-        geodetic_df = DataFrame()
+        # Carry `df`'s own index, for the reason given in `geohash_decode`.
+        geodetic_df = DataFrame(index=df.index)
         (
             geodetic_df["latitude"],
             geodetic_df["longitude"],
diff --git a/tests/unit_tests/pandas_postprocessing/test_geography.py b/tests/unit_tests/pandas_postprocessing/test_geography.py
index eab9eefe6a9..938cc5b2cfd 100644
--- a/tests/unit_tests/pandas_postprocessing/test_geography.py
+++ b/tests/unit_tests/pandas_postprocessing/test_geography.py
@@ -14,10 +14,13 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+import pytest
+
 from superset.utils.pandas_postprocessing import (
     geodetic_parse,
     geohash_decode,
     geohash_encode,
+    pivot,
 )
 from tests.unit_tests.fixtures.dataframes import lonlat_df
 from tests.unit_tests.pandas_postprocessing.utils import round_floats, series_to_list
@@ -129,3 +132,81 @@ def test_geohash_encode_drops_working_columns():
     assert post_df.columns.tolist() == ["city", "latitude", "longitude", "hash"]
     assert not post_df.columns.duplicated().any()
     assert series_to_list(post_df["hash"]) == series_to_list(lonlat_df["geohash"])
+
+
+@pytest.mark.parametrize(
+    "func, source_column, kwargs",
+    [
+        (geohash_decode, "geohash", {"geohash": "geohash"}),
+        (geodetic_parse, "geodetic", {"geodetic": "geodetic"}),
+        (
+            geodetic_parse,
+            "geodetic",
+            {"geodetic": "geodetic", "altitude": "altitude"},
+        ),
+    ],
+    ids=["geohash_decode", "geodetic_parse", "geodetic_parse-with-altitude"],
+)
+def test_geography_keeps_the_index_it_was_given(func, source_column, kwargs):
+    """
+    A parsed frame must carry the caller's index, not a fresh one.
+
+    `_append_columns` aligns on the index, so a parsed frame left on its own
+    `RangeIndex` matches nothing when the caller's frame is indexed by anything
+    else. `pd.concat` then unions the two, and the result holds the original
+    rows with no coordinates plus one unlabelled row per parsed row.
+    """
+    df = lonlat_df[["city", source_column]].set_index("city")
+
+    post_df = func(df=df, latitude="lat", longitude="lon", **kwargs)
+
+    assert len(post_df) == len(df)
+    assert post_df.index.tolist() == df.index.tolist()
+    assert post_df["lat"].notna().all()
+    assert post_df["lon"].notna().all()
+
+
+def test_geohash_encode_keeps_the_index_it_was_given():
+    """
+    Encoding is unaffected by the index, and must stay that way.
+
+    It slices `df[[latitude, longitude]]`, so its working frame inherits the
+    caller's index rather than getting a fresh one, which is why it needs no
+    change where the two parsers did. Pinned so a later refactor that builds a
+    frame here instead does not reintroduce the mismatch.
+    """
+    df = lonlat_df[["city", "latitude", "longitude"]].set_index("city")
+
+    post_df = geohash_encode(
+        df=df, latitude="latitude", longitude="longitude", geohash="geohash"
+    )
+
+    assert len(post_df) == len(df)
+    assert post_df.index.tolist() == df.index.tolist()
+    assert series_to_list(post_df["geohash"]) == series_to_list(lonlat_df["geohash"])
+
+
+def test_geodetic_parse_after_pivot_keeps_one_row_per_group():
+    """
+    The index a previous operation leaves behind must survive the parse.
+
+    `pivot` indexes by its groupby columns, so a chain that pivots and then
+    parses is the shortest route to a non-`RangeIndex` frame reaching these
+    operations. Before the index was carried, two rows in became four out, with
+    the coordinates landing on the two phantom rows.
+    """
+    pivoted = pivot(
+        df=lonlat_df[["city", "geodetic"]],
+        index=["city"],
+        columns=[],
+        aggregates={"geodetic": {"operator": "max"}},
+    )
+
+    post_df = geodetic_parse(
+        df=pivoted, geodetic="geodetic", latitude="lat", longitude="lon"
+    )
+
+    assert len(post_df) == len(pivoted)
+    assert post_df.index.tolist() == pivoted.index.tolist()
+    assert post_df["lat"].notna().all()
+    assert post_df["lon"].notna().all()