Skip to content

Commit

Permalink
fix: use nw.Unknown for unknown dtypes (#904)
Browse files Browse the repository at this point in the history
* use nw.Unknown for unknown dtypes

* cov

* cov
  • Loading branch information
MarcoGorelli authored Sep 3, 2024
1 parent 6540836 commit cb8a5a2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion narwhals/_arrow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def translate_dtype(dtype: Any) -> dtypes.DType:
return dtypes.Duration()
if pa.types.is_dictionary(dtype):
return dtypes.Categorical()
raise AssertionError
return dtypes.Unknown() # pragma: no cover


def narwhals_to_native_dtype(dtype: dtypes.DType | type[dtypes.DType]) -> Any:
Expand Down
7 changes: 1 addition & 6 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ def map_duckdb_dtype_to_narwhals_dtype(
return dtypes.Boolean()
if duckdb_dtype == "INTERVAL":
return dtypes.Duration()
msg = ( # pragma: no cover
f"Invalid dtype, got: {duckdb_dtype}.\n\n"
"If you believe this dtype should be supported in Narwhals, "
"please report an issue at https://github.com/narwhals-dev/narwhals."
)
raise AssertionError(msg)
return dtypes.Unknown()


class DuckDBInterchangeFrame:
Expand Down
7 changes: 1 addition & 6 deletions narwhals/_ibis/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ def map_ibis_dtype_to_narwhals_dtype(
return dtypes.Date()
if ibis_dtype.is_timestamp():
return dtypes.Datetime()
msg = ( # pragma: no cover
f"Invalid dtype, got: {ibis_dtype}.\n\n"
"If you believe this dtype should be supported in Narwhals, "
"please report an issue at https://github.com/narwhals-dev/narwhals."
)
raise AssertionError(msg)
return dtypes.Unknown() # pragma: no cover


class IbisInterchangeFrame:
Expand Down
8 changes: 8 additions & 0 deletions tests/frame/interchange_schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import timedelta

import duckdb
import pandas as pd
import polars as pl
import pytest

Expand Down Expand Up @@ -211,3 +212,10 @@ def test_get_level() -> None:
nw.get_level(nw.from_native(df.__dataframe__(), eager_or_interchange_only=True))
== "interchange"
)


def test_unknown_dtype() -> None:
df = pd.DataFrame({"a": [1, 2, 3]})
rel = duckdb.from_df(df).select("cast(a as int128) as a")
result = nw.from_native(rel).schema
assert result == {"a": nw.Unknown}

0 comments on commit cb8a5a2

Please sign in to comment.