Skip to content

fix: Set Ibis time_unit and time_zone for Datetime dtype conversion #2709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions narwhals/_ibis/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from functools import lru_cache
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING, Any, Literal, cast

import ibis
import ibis.expr.datatypes as ibis_dtypes
Expand All @@ -12,6 +12,7 @@
from collections.abc import Mapping

import ibis.expr.types as ir
from ibis.common.temporal import TimestampUnit
from ibis.expr.datatypes import DataType as IbisDataType
from typing_extensions import TypeAlias, TypeIs

Expand Down Expand Up @@ -111,8 +112,9 @@ def native_to_narwhals_dtype(ibis_dtype: IbisDataType, version: Version) -> DTyp
return dtypes.String()
if ibis_dtype.is_date():
return dtypes.Date()
if ibis_dtype.is_timestamp():
return dtypes.Datetime()
if is_timestamp(ibis_dtype):
_unit = cast("TimestampUnit", ibis_dtype.unit)
return dtypes.Datetime(time_unit=_unit.value, time_zone=ibis_dtype.timezone)
if is_interval(ibis_dtype):
_time_unit = ibis_dtype.unit.value
if _time_unit not in {"ns", "us", "ms", "s"}: # pragma: no cover
Expand Down Expand Up @@ -143,6 +145,10 @@ def native_to_narwhals_dtype(ibis_dtype: IbisDataType, version: Version) -> DTyp
return dtypes.Unknown() # pragma: no cover


def is_timestamp(obj: IbisDataType) -> TypeIs[ibis_dtypes.Timestamp]:
return obj.is_timestamp()


def is_interval(obj: IbisDataType) -> TypeIs[ibis_dtypes.Interval]:
return obj.is_interval()

Expand Down Expand Up @@ -200,7 +206,7 @@ def narwhals_to_native_dtype( # noqa: C901, PLR0912
msg = "Categorical not supported by Ibis"
raise NotImplementedError(msg)
if isinstance_or_issubclass(dtype, dtypes.Datetime):
return ibis_dtypes.Timestamp()
return ibis_dtypes.Timestamp.from_unit(dtype.time_unit, timezone=dtype.time_zone)
if isinstance_or_issubclass(dtype, dtypes.Duration):
return ibis_dtypes.Interval(unit=dtype.time_unit) # pyright: ignore[reportArgumentType]
if isinstance_or_issubclass(dtype, dtypes.Date):
Expand Down
Loading