Skip to content

Commit

Permalink
fix: timezone issue in Pandas 2 (#24955)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored and pull[bot] committed May 16, 2024
1 parent f4d4ed0 commit 19d1dd5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 4 additions & 5 deletions superset/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,11 @@ def __init__( # pylint: disable=too-many-locals
try:
if sample.tzinfo:
tz = sample.tzinfo
series = pd.Series(
array[column], dtype="datetime64[ns]"
)
series = pd.to_datetime(series).dt.tz_localize(tz)
series = pd.Series(array[column])
series = pd.to_datetime(series)
pa_data[i] = pa.Array.from_pandas(
series, type=pa.timestamp("ns", tz=tz)
series,
type=pa.timestamp("ns", tz=tz),
)
except Exception as ex: # pylint: disable=broad-except
logger.exception(ex)
Expand Down
26 changes: 25 additions & 1 deletion tests/unit_tests/result_set_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

# pylint: disable=import-outside-toplevel, unused-argument

from datetime import datetime, timezone

import numpy as np
import pandas as pd
from numpy.core.multiarray import array
from pytest_mock import MockerFixture

from superset.result_set import stringify_values
from superset.db_engine_specs.base import BaseEngineSpec
from superset.result_set import stringify_values, SupersetResultSet


def test_column_names_as_bytes() -> None:
Expand Down Expand Up @@ -140,3 +143,24 @@ def test_stringify_with_null_timestamps():
)

assert np.array_equal(result_set, expected)


def test_timezone_series(mocker: MockerFixture) -> None:
"""
Test that we can handle timezone-aware datetimes correctly.
This covers a regression that happened when upgrading from Pandas 1.5.3 to 2.0.3.
"""
logger = mocker.patch("superset.result_set.logger")

data = [[datetime(2023, 1, 1, tzinfo=timezone.utc)]]
description = [(b"__time", "datetime", None, None, None, None, False)]
result_set = SupersetResultSet(
data,
description, # type: ignore
BaseEngineSpec,
)
assert result_set.to_pandas_df().values.tolist() == [
[pd.Timestamp("2023-01-01 00:00:00+0000", tz="UTC")]
]
logger.exception.assert_not_called()

0 comments on commit 19d1dd5

Please sign in to comment.