Skip to content

Commit

Permalink
feat: Respect input time zone if input is pandas Timestamp (#18346)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Aug 25, 2024
1 parent dd1fc86 commit 134c49f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/_utils/construction/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def sequence_to_pyseries(
python_dtype = type(value)

# temporal branch
if python_dtype in py_temporal_types:
if issubclass(python_dtype, tuple(py_temporal_types)):
if dtype is None:
dtype = parse_into_dtype(python_dtype) # construct from integer
elif dtype in py_temporal_types:
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/datatypes/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def parse_py_type_into_dtype(input: PythonDataType | type[object]) -> PolarsData
return String()
elif input is bool:
return Boolean()
elif input is date:
return Date()
elif input is datetime:
elif isinstance(input, type) and issubclass(input, datetime): # type: ignore[redundant-expr]
return Datetime("us")
elif isinstance(input, type) and issubclass(input, date): # type: ignore[redundant-expr]
return Date()
elif input is timedelta:
return Duration
elif input is time:
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/constructors/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Any

import numpy as np
import pandas as pd
import pytest

import polars as pl
Expand Down Expand Up @@ -148,6 +149,12 @@ def test_series_init_np_temporal_with_nat_15518() -> None:
assert_series_equal(result, expected)


def test_series_init_pandas_timestamp_18127() -> None:
result = pl.Series([pd.Timestamp("2000-01-01T00:00:00.123456789", tz="UTC")])
# Note: time unit is not (yet) respected, it should be Datetime('ns', 'UTC').
assert result.dtype == pl.Datetime("us", "UTC")


def test_series_init_np_2d_zero_zero_shape() -> None:
arr = np.array([]).reshape(0, 0)
with pytest.raises(
Expand Down

0 comments on commit 134c49f

Please sign in to comment.