This is an automated email from the ASF dual-hosted git repository. eschutho pushed a commit to branch fix/pandas-replace-inf-downcast-warning in repository https://gitbox.apache.org/repos/asf/superset.git
commit eb76410e2679804695d8f047e04ef03d043d0a1d Author: Elizabeth Thompson <[email protected]> AuthorDate: Fri Oct 2 16:03:35 2026 +0000 fix(models): silence pandas silent-downcasting FutureWarning in normalize_df df.replace([inf, -inf], nan, inplace=True) emits a FutureWarning on object columns because pandas deprecated silent downcasting inside replace(). Opt out of the implicit downcast for the replace and call infer_objects() explicitly, which keeps the resulting dtypes identical. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- superset/models/helpers.py | 6 +++++- tests/unit_tests/models/helpers_test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 47e10c8b708..5254b926e04 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -2635,7 +2635,11 @@ 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) + # Opt out of pandas' deprecated silent downcasting inside ``replace`` and + # then downcast explicitly, which keeps the existing dtype behavior. + with pd.option_context("future.no_silent_downcasting", True): + df.replace([np.inf, -np.inf], np.nan, inplace=True) + df = df.infer_objects(copy=False) return df diff --git a/tests/unit_tests/models/helpers_test.py b/tests/unit_tests/models/helpers_test.py index 6990fd008c1..0f6a88e348e 100644 --- a/tests/unit_tests/models/helpers_test.py +++ b/tests/unit_tests/models/helpers_test.py @@ -4121,6 +4121,34 @@ 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), + "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["label"].dtype == object + + @pytest.mark.parametrize( "datetime_format,value", [
