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

(superset) branch master updated: fix(excel): handle duplicate column labels in xlsx export (#43561)

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

michael-s-molina 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 a140e74f5f5 fix(excel): handle duplicate column labels in xlsx export (#43561)
a140e74f5f5 is described below

commit a140e74f5f54b2ada25e7558d884812facd3375d
Author: Luiz Otavio <45...@users.noreply.github.com>
AuthorDate: Wed Aug 26 16:39:03 2026 -0300

    fix(excel): handle duplicate column labels in xlsx export (#43561)
    
    Co-authored-by: Claude Opus 5 <no...@anthropic.com>
---
 superset/utils/excel.py               | 41 ++++++++++++++++++-----
 tests/unit_tests/utils/excel_tests.py | 63 +++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 8 deletions(-)

diff --git a/superset/utils/excel.py b/superset/utils/excel.py
index b370c5bb2fd..a5ea7feed4b 100644
--- a/superset/utils/excel.py
+++ b/superset/utils/excel.py
@@ -60,8 +60,22 @@ def quote_formulas(df: pd.DataFrame) -> pd.DataFrame:
     """
     Make sure to quote any formulas for security reasons.
     """
-    for col in df.select_dtypes(include="object").columns:
-        df[col] = df[col].apply(_quote_formula)
+    # Columns are addressed by position rather than by label: a dataframe can
+    # carry duplicate column labels (the verbose_map rename in
+    # QueryContextProcessor.get_data can collapse two columns onto the same
+    # name), and ``df[label]`` then yields a DataFrame instead of a Series.
+    # ``DataFrame.apply`` would hand whole columns to the mapper rather than
+    # individual cells, silently leaving formulas unquoted.
+    for idx in range(len(df.columns)):
+        series = df.iloc[:, idx]
+        # ``is_string_dtype`` rather than an ``object`` comparison: pandas 3
+        # gives string columns a dedicated ``str`` dtype, which an object-only
+        # check (as the ``select_dtypes(include="object")`` this replaced) would
+        # skip, silently leaving formulas unquoted.
+        if pd.api.types.is_object_dtype(series.dtype) or pd.api.types.is_string_dtype(
+            series.dtype
+        ):
+            df.isetitem(idx, series.map(_quote_formula))
 
     # Column headers and index labels are written to the sheet as well, and
     # pivot exports promote data values into both (a hostile warehouse string
@@ -104,20 +118,31 @@ def apply_column_types(
     :param column_types: The types of the columns
     :return: The dataframe with the column types applied
     """
-    for column, column_type in zip(df.columns, column_types, strict=False):
+    # Columns are addressed by position for the same reason as in
+    # ``quote_formulas``: duplicate column labels make ``df[label]`` return a
+    # DataFrame, and ``DataFrame`` has no ``dtype``. Slicing column_types keeps
+    # the lenient pairing the previous ``zip(..., strict=False)`` provided.
+    for idx, column_type in enumerate(column_types[: len(df.columns)]):
+        series = df.iloc[:, idx]
         if column_type == GenericDataType.NUMERIC:
             try:
-                df[column] = pd.to_numeric(df[column])
+                series = pd.to_numeric(series)
                 # if the number is too large, convert it to a string
                 # Excel does not support numbers larger than 10^15
-                df[column] = df[column].apply(
+                series = series.apply(
                     lambda x: (
                         str(x) if isinstance(x, (int, float)) and abs(x) > 10**15 else x
                     )
                 )
             except ValueError:
-                df[column] = df[column].astype(str)
-        elif isinstance(df[column].dtype, pd.DatetimeTZDtype):
+                series = series.astype(str)
+        elif isinstance(series.dtype, pd.DatetimeTZDtype):
             # timezones are not supported
-            df[column] = df[column].astype(str)
+            series = series.astype(str)
+        else:
+            continue
+        # ``isetitem`` replaces the column at that position, which is both
+        # unambiguous under duplicate labels and free of the in-place dtype
+        # casting that ``iloc`` assignment attempts.
+        df.isetitem(idx, series)
     return df
diff --git a/tests/unit_tests/utils/excel_tests.py b/tests/unit_tests/utils/excel_tests.py
index 4320df19f3d..43f71f77317 100644
--- a/tests/unit_tests/utils/excel_tests.py
+++ b/tests/unit_tests/utils/excel_tests.py
@@ -186,6 +186,69 @@ def test_column_data_types_with_failing_conversion():
     assert not is_numeric_dtype(df["col3"])
 
 
+def test_apply_column_types_with_duplicate_column_labels() -> None:
+    """
+    Test that duplicate column labels do not break the export.
+
+    The verbose_map rename in QueryContextProcessor.get_data can collapse two
+    columns onto the same label, which used to raise
+    "'DataFrame' object has no attribute 'dtype'".
+    """
+    df = pd.DataFrame(
+        [
+            ["1", datetime(2023, 1, 1, 0, 0, tzinfo=timezone.utc), "2"],
+            ["3", datetime(2023, 1, 2, 0, 0, tzinfo=timezone.utc), "4"],
+        ],
+        columns=["dupe", "dupe", "other"],
+    )
+    coltypes: list[GenericDataType] = [
+        GenericDataType.STRING,
+        GenericDataType.TEMPORAL,
+        GenericDataType.NUMERIC,
+    ]
+
+    apply_column_types(df, coltypes)
+
+    # each position is typed independently, despite sharing a label
+    assert not is_numeric_dtype(df.iloc[:, 0])
+    assert df.iloc[:, 1].tolist() == [
+        "2023-01-01 00:00:00+00:00",
+        "2023-01-02 00:00:00+00:00",
+    ]
+    assert is_numeric_dtype(df.iloc[:, 2])
+
+    contents = df_to_excel(df, index=False)
+    assert pd.read_excel(contents).shape == (2, 3)
+
+
+def test_quote_formulas_with_duplicate_column_labels() -> None:
+    """
+    Test that formulas are quoted even when column labels are duplicated.
+    """
+    df = pd.DataFrame(
+        [["=SUM(A1:A2)", "@SUM(A1:A2)", "normal"]],
+        columns=["dupe", "dupe", "other"],
+    )
+
+    result = quote_formulas(df)
+
+    assert result.iloc[0].tolist() == ["'=SUM(A1:A2)", "'@SUM(A1:A2)", "normal"]
+
+
+def test_quote_formulas_with_dedicated_string_dtype() -> None:
+    """
+    Test that formulas are quoted in columns using the dedicated string dtype.
+
+    pandas 3 gives string columns a ``str`` dtype rather than ``object``, so an
+    object-only dtype check would skip them and leave formulas unquoted.
+    """
+    df = pd.DataFrame({"formula": pd.array(["=SUM(A1:A2)", "normal"], dtype="string")})
+
+    result = quote_formulas(df)
+
+    assert result["formula"].tolist() == ["'=SUM(A1:A2)", "normal"]
+
+
 def test_column_data_types_with_large_numeric_values():
     df = pd.DataFrame(
         {