Skip to content

Commit

Permalink
feat(python): return Datetime/Duration with appropriate timeunit when…
Browse files Browse the repository at this point in the history
… inferring from pytype (#5127)
  • Loading branch information
alexander-beedie committed Oct 6, 2022
1 parent 1a38e6b commit 798882d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ def __hash__(self) -> int:
str: Utf8,
bool: Boolean,
date: Date,
datetime: Datetime,
timedelta: Duration,
datetime: Datetime("us"),
timedelta: Duration("us"),
time: Time,
list: List,
tuple: List,
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inspect
import pickle
from datetime import datetime, timedelta

import polars as pl
from polars import datatypes
Expand Down Expand Up @@ -30,6 +31,14 @@ def test_dtype_temporal_units() -> None:
assert pl.Datetime("ms") != pl.Datetime("ns")
assert pl.Duration("ns") != pl.Duration("us")

# check timeunit from pytype
for inferred_dtype, expected_dtype in (
(datatypes.py_type_to_dtype(datetime), pl.Datetime),
(datatypes.py_type_to_dtype(timedelta), pl.Duration),
):
assert inferred_dtype == expected_dtype
assert inferred_dtype.tu == "us" # type: ignore[union-attr]


def test_get_idx_type() -> None:
assert datatypes.get_idx_type() == datatypes.UInt32
Expand Down

0 comments on commit 798882d

Please sign in to comment.