Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 13 additions & 7 deletions narwhals/_polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
BACKEND_VERSION = Implementation.POLARS._backend_version()
"""Static backend version for `polars`."""

HAS_UINT_128 = BACKEND_VERSION >= (1, 34, 0)
"""https://github.com/pola-rs/polars/pull/24346"""

HAS_INT_128 = BACKEND_VERSION >= (1, 18, 0)
"""https://github.com/pola-rs/polars/pull/20232"""
Comment thread
FBruzzesi marked this conversation as resolved.
Outdated

SERIES_RESPECTS_DTYPE: Final[bool] = BACKEND_VERSION >= (0, 20, 26)
"""`pl.Series(dtype=...)` fixed in https://github.com/pola-rs/polars/pull/15962

Expand Down Expand Up @@ -93,8 +99,7 @@ def native_to_narwhals_dtype( # noqa: C901, PLR0912
return dtypes.Float64()
if dtype == pl.Float32:
return dtypes.Float32()
if hasattr(pl, "Int128") and dtype == pl.Int128: # pragma: no cover
# Not available for Polars pre 1.8.0
if HAS_INT_128 and dtype == pl.Int128:
return dtypes.Int128()
if dtype == pl.Int64:
return dtypes.Int64()
Expand All @@ -104,8 +109,7 @@ def native_to_narwhals_dtype( # noqa: C901, PLR0912
return dtypes.Int16()
if dtype == pl.Int8:
return dtypes.Int8()
if hasattr(pl, "UInt128") and dtype == pl.UInt128: # pragma: no cover
# Not available for Polars pre 1.8.0
if HAS_UINT_128 and hasattr(pl, "UInt128") and dtype == pl.UInt128: # pyright: ignore[reportAttributeAccessIssue] # pragma: no cover
Comment thread
dangotbanned marked this conversation as resolved.
Outdated
return dtypes.UInt128()
if dtype == pl.UInt64:
return dtypes.UInt64()
Expand Down Expand Up @@ -163,6 +167,9 @@ def native_to_narwhals_dtype( # noqa: C901, PLR0912


dtypes = Version.MAIN.dtypes
_version_dependent: dict[type[DType], pl.DataType] = {}
if HAS_INT_128:
_version_dependent[dtypes.Int128] = pl.Int128()
NW_TO_PL_DTYPES: Mapping[type[DType], pl.DataType] = {
Comment thread
dangotbanned marked this conversation as resolved.
Outdated
dtypes.Float64: pl.Float64(),
dtypes.Float32: pl.Float32(),
Expand All @@ -182,8 +189,10 @@ def native_to_narwhals_dtype( # noqa: C901, PLR0912
dtypes.UInt64: pl.UInt64(),
dtypes.Object: pl.Object(),
dtypes.Unknown: pl.Unknown(),
**_version_dependent,
}
UNSUPPORTED_DTYPES = (dtypes.Decimal,)
del _version_dependent


def narwhals_to_native_dtype( # noqa: C901
Expand All @@ -193,9 +202,6 @@ def narwhals_to_native_dtype( # noqa: C901
base_type = dtype.base_type()
if pl_type := NW_TO_PL_DTYPES.get(base_type):
return pl_type
if dtype == dtypes.Int128 and hasattr(pl, "Int128"):
# Not available for Polars pre 1.8.0
return pl.Int128()
if isinstance_or_issubclass(dtype, dtypes.Enum):
if version is Version.V1:
msg = "Converting to Enum is not supported in narwhals.stable.v1"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typing = [ # keep some of these pinned and bump periodically so there's fewer s
"pyright",
"pyarrow-stubs==19.2",
"sqlframe",
"polars==1.32.2",
"polars==1.33.1",
"uv",
"narwhals[ibis]",
]
Expand Down
Loading