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/20 01:30:57 UTC

(superset) branch master updated: fix(dataset): convert timezone-aware values with the dataset timezone (#45344)

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 152e1e9de9a fix(dataset): convert timezone-aware values with the dataset timezone (#45344)
152e1e9de9a is described below

commit 152e1e9de9a9e7d157e38ea99256068df621f76d
Author: Bishop Kammeraad <11...@users.noreply.github.com>
AuthorDate: Mon Oct 19 21:30:49 2026 -0400

    fix(dataset): convert timezone-aware values with the dataset timezone (#45344)
---
 superset/utils/core.py              | 11 +++++++----
 tests/unit_tests/utils/test_core.py | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/superset/utils/core.py b/superset/utils/core.py
index f92b05e43f7..475c70b12f6 100644
--- a/superset/utils/core.py
+++ b/superset/utils/core.py
@@ -2182,13 +2182,16 @@ def normalize_dttm_col(
         if _col.timezone and isinstance(_col.timezone, str):
             try:
                 tz = ZoneInfo(_col.timezone)
-                # Data is stored in UTC, convert to the dataset's configured timezone
-                # First make the datetime UTC-aware, then convert to target timezone
+                # Naive values are assumed to be UTC; values that already carry
+                # a timezone are converted from it directly
                 series = df[_col.col_label]
                 if not series.empty and series.notna().any():
-                    # Convert UTC to target timezone
                     df[_col.col_label] = (
-                        series.dt.tz_localize("UTC")
+                        (
+                            series.dt.tz_localize("UTC")
+                            if series.dt.tz is None
+                            else series
+                        )
                         .dt.tz_convert(tz)
                         .dt.tz_localize(None)  # Remove timezone info for display
                     )
diff --git a/tests/unit_tests/utils/test_core.py b/tests/unit_tests/utils/test_core.py
index 6439e771edb..d7b849dd5cc 100644
--- a/tests/unit_tests/utils/test_core.py
+++ b/tests/unit_tests/utils/test_core.py
@@ -473,6 +473,26 @@ def test_normalize_dttm_col_timezone_handles_dst() -> None:
     assert df["date_col"][0].strftime("%Y-%m-%d %H:%M:%S") == "2020-07-01 02:00:00"
 
 
+def test_normalize_dttm_col_timezone_with_tz_aware_values() -> None:
+    """Values that already carry a timezone are converted, not re-localized."""
+    df = pd.DataFrame(
+        {
+            "date_col": pd.to_datetime(
+                ["2020-01-01 00:00:00+00:00", "2020-07-01 05:00:00+05:00"],
+                utc=True,
+            )
+        }
+    )
+    dttm_cols = (DateColumn(col_label="date_col", timezone="Europe/Berlin"),)
+
+    normalize_dttm_col(df, dttm_cols)
+
+    assert is_datetime64_dtype(df["date_col"])
+    assert df["date_col"][0].tzinfo is None
+    assert df["date_col"][0].strftime("%Y-%m-%d %H:%M:%S") == "2020-01-01 01:00:00"
+    assert df["date_col"][1].strftime("%Y-%m-%d %H:%M:%S") == "2020-07-01 02:00:00"
+
+
 def test_normalize_dttm_col_timezone_takes_precedence_over_offset() -> None:
     """When both timezone and offset are set, the timezone conversion wins."""
     df = pd.DataFrame({"date_col": ["2020-01-01 00:00:00"]})