You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2026/10/05 22:01:55 UTC

(superset) branch master updated: fix(models): silence pandas silent-downcasting FutureWarning in normalize_df (#44897)

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

eschutho 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 0c6373cd9f0 fix(models): silence pandas silent-downcasting FutureWarning in normalize_df (#44897)
0c6373cd9f0 is described below

commit 0c6373cd9f01326ac2cfe0bd96579d4daa8c8849
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Mon Oct 5 15:01:47 2026 -0700

    fix(models): silence pandas silent-downcasting FutureWarning in normalize_df (#44897)
    
    Co-authored-by: Claude Sonnet 5.5 <no...@anthropic.com>
---
 superset/models/helpers.py              |  4 +++-
 tests/unit_tests/models/helpers_test.py | 30 ++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/superset/models/helpers.py b/superset/models/helpers.py
index de1df53923a..675cbf54d1f 100644
--- a/superset/models/helpers.py
+++ b/superset/models/helpers.py
@@ -2635,7 +2635,9 @@ class ExploreMixin:  # pylint: disable=too-many-public-methods
         if getattr(self, "enforce_numerical_metrics", True):
             dataframe_utils.df_metrics_to_num(df, query_object)
 
-        df.replace([np.inf, -np.inf], np.nan, inplace=True)
+        # ``mask`` + ``infer_objects`` rather than ``replace``, which emits a
+        # FutureWarning about deprecated silent downcasting on object columns.
+        df = df.mask(df.isin([np.inf, -np.inf])).infer_objects()
 
         return df
 
diff --git a/tests/unit_tests/models/helpers_test.py b/tests/unit_tests/models/helpers_test.py
index f24a109605c..f50f62be693 100644
--- a/tests/unit_tests/models/helpers_test.py
+++ b/tests/unit_tests/models/helpers_test.py
@@ -4121,6 +4121,36 @@ def test_normalize_df_applies_python_date_format_to_unaggregated_columns(
     assert result["ts"][2].strftime("%Y-%m-%d") == "2022-01-01"
 
 
+def test_normalize_df_replaces_infinity_without_downcasting_warning() -> None:
+    """Replacing +/-inf in an object column must not emit pandas' silent
+    downcasting ``FutureWarning``, and must still yield a float column."""
+    import warnings
+
+    import numpy as np
+    import pandas as pd
+
+    ts_col = MagicMock(
+        column_name="ts", is_dttm=True, python_date_format=None, datetime_format=None
+    )
+    datasource = _normalize_df_datasource(ts_col)
+    df = pd.DataFrame(
+        {
+            "metric": pd.Series([1.0, np.inf, -np.inf, 2.0], dtype=object),
+            "count": pd.Series([1, 2, -np.inf, 4], dtype=object),
+            "label": ["a", "b", "c", "d"],
+        }
+    )
+
+    with warnings.catch_warnings():
+        warnings.simplefilter("error", FutureWarning)
+        result = datasource.normalize_df(df, _raw_query_object())
+
+    assert result["metric"].dtype == np.float64
+    assert result["metric"].isna().tolist() == [False, True, True, False]
+    assert result["count"].dtype == np.float64
+    assert result["label"].dtype == object
+
+
 @pytest.mark.parametrize(
     "datetime_format,value",
     [