Skip to content

Commit 027c2c2

Browse files
authored
Fix plc.Scalar.from_py(datetime.datetime) incorrectly localizing naive datetimes (#20769)
closes pola-rs/polars#25549 closes #19044 Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Matthew Murray (https://github.com/Matt711) URL: #20769
1 parent c5e3dcd commit 027c2c2

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

python/cudf_polars/tests/test_select.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def test_select_with_cse_with_agg():
9292
assert_gpu_result_equal(query)
9393

9494

95+
def test_select_native_datetime():
96+
df = pl.LazyFrame({"c0": [1]})
97+
query = df.select(pl.datetime(1969, 12, 7, 20, 47, 14))
98+
assert_gpu_result_equal(query)
99+
100+
95101
@pytest.mark.parametrize("fmt", ["ndjson", "csv"])
96102
def test_select_fast_count_unsupported_formats(tmp_path, fmt):
97103
df = pl.DataFrame({"a": [1, 2, 3]})

python/pylibcudf/pylibcudf/scalar.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,12 @@ def _(
707707
cdef DataType c_dtype = dtype
708708
cdef type_id tid = c_dtype.id()
709709
if isinstance(py_val, datetime.datetime):
710-
epoch_seconds = py_val.timestamp()
710+
if py_val.tzinfo is None:
711+
# Treat tz-naive datetime as UTC so .timestamp()
712+
# does not account for the system's timezone.
713+
epoch_seconds = py_val.replace(tzinfo=datetime.timezone.utc).timestamp()
714+
else:
715+
epoch_seconds = py_val.timestamp()
711716
else:
712717
epoch_seconds = (py_val - datetime.date(1970, 1, 1)).total_seconds()
713718
if tid == type_id.TIMESTAMP_NANOSECONDS:

python/pylibcudf/tests/test_scalar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_to_py(py_scalar):
6565
if isinstance(py_scalar, (datetime.datetime, datetime.timedelta)):
6666
with pytest.raises(NotImplementedError):
6767
plc.Scalar.from_py(py_scalar).to_py()
68+
assert py_scalar == plc.Scalar.from_py(py_scalar).to_arrow().as_py()
6869
else:
6970
assert py_scalar == plc.Scalar.from_py(py_scalar).to_py()
7071

0 commit comments

Comments
 (0)