-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d246b7
commit 780e2bc
Showing
7 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
||
from narwhals import dtypes | ||
|
||
if TYPE_CHECKING: | ||
from narwhals._duckdb.series import DuckDBInterchangeSeries | ||
|
||
|
||
def map_duckdb_dtype_to_narwhals_dtype( | ||
duckdb_dtype: Any, | ||
) -> dtypes.DType: | ||
if duckdb_dtype == "BIGINT": | ||
return dtypes.Int64() | ||
if duckdb_dtype == "INTEGER": | ||
return dtypes.Int32() | ||
if duckdb_dtype == "SMALLINT": | ||
return dtypes.Int16() | ||
if duckdb_dtype == "TINYINT": | ||
return dtypes.Int8() | ||
if duckdb_dtype == "UBIGINT": | ||
return dtypes.UInt64() | ||
if duckdb_dtype == "UINTEGER": | ||
return dtypes.UInt32() | ||
if duckdb_dtype == "USMALLINT": | ||
return dtypes.UInt16() | ||
if duckdb_dtype == "UTINYINT": | ||
return dtypes.UInt8() | ||
if duckdb_dtype == "DOUBLE": | ||
return dtypes.Float64() | ||
if duckdb_dtype == "FLOAT": | ||
return dtypes.Float32() | ||
if duckdb_dtype == "VARCHAR": | ||
return dtypes.String() | ||
if duckdb_dtype == "DATE": | ||
return dtypes.Date() | ||
if duckdb_dtype == "TIMESTAMP": | ||
return dtypes.Datetime() | ||
if duckdb_dtype == "BOOLEAN": | ||
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) | ||
|
||
|
||
class DuckDBInterchangeFrame: | ||
def __init__(self, df: Any) -> None: | ||
self._native_frame = df | ||
|
||
def __narwhals_dataframe__(self) -> Any: | ||
return self | ||
|
||
def __getitem__(self, item: str) -> DuckDBInterchangeSeries: | ||
from narwhals._duckdb.series import DuckDBInterchangeSeries | ||
|
||
return DuckDBInterchangeSeries(self._native_frame.select(item)) | ||
|
||
def __getattr__(self, attr: str) -> Any: | ||
if attr == "schema": | ||
return { | ||
column_name: map_duckdb_dtype_to_narwhals_dtype(duckdb_dtype) | ||
for column_name, duckdb_dtype in zip( | ||
self._native_frame.columns, self._native_frame.types | ||
) | ||
} | ||
|
||
msg = ( # pragma: no cover | ||
f"Attribute {attr} is not supported for metadata-only dataframes.\n\n" | ||
"If you would like to see this kind of object better supported in " | ||
"Narwhals, please open a feature request " | ||
"at https://github.com/narwhals-dev/narwhals/issues." | ||
) | ||
raise NotImplementedError(msg) # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from narwhals._duckdb.dataframe import map_duckdb_dtype_to_narwhals_dtype | ||
|
||
|
||
class DuckDBInterchangeSeries: | ||
def __init__(self, df: Any) -> None: | ||
self._native_series = df | ||
|
||
def __narwhals_series__(self) -> Any: | ||
return self | ||
|
||
def __getattr__(self, attr: str) -> Any: | ||
if attr == "dtype": | ||
return map_duckdb_dtype_to_narwhals_dtype(self._native_series.types[0]) | ||
msg = ( # pragma: no cover | ||
f"Attribute {attr} is not supported for metadata-only dataframes.\n\n" | ||
"If you would like to see this kind of object better supported in " | ||
"Narwhals, please open a feature request " | ||
"at https://github.com/narwhals-dev/narwhals/issues." | ||
) | ||
raise NotImplementedError(msg) # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
covdefaults | ||
duckdb | ||
pandas | ||
polars | ||
pre-commit | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters