Skip to content
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

Feat/add collect schema to interchange dfs #1646

Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,11 @@ def _change_version(self: Self, version: Version) -> Self:

def _from_native_frame(self: Self, df: Any) -> Self:
return self.__class__(df, version=self._version)

def collect_schema(self) -> dict[str, DType]:
return {
column_name: native_to_narwhals_dtype(str(duckdb_dtype), self._version)
for column_name, duckdb_dtype in zip(
self._native_frame.columns, self._native_frame.types
)
}
6 changes: 6 additions & 0 deletions narwhals/_ibis/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@ def _change_version(self: Self, version: Version) -> Self:

def _from_native_frame(self: Self, df: Any) -> Self:
return self.__class__(df, version=self._version)

def collect_schema(self) -> dict[str, DType]:
return {
column_name: native_to_narwhals_dtype(ibis_dtype, self._version)
for column_name, ibis_dtype in self._native_frame.schema().items()
}
2 changes: 2 additions & 0 deletions tests/frame/interchange_schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_interchange_schema_ibis(
assert result == expected
assert df["a"].dtype == nw.Int64
assert df.columns == list(expected.keys())
assert df.collect_schema() == expected


def test_interchange_schema_duckdb() -> None:
Expand Down Expand Up @@ -221,6 +222,7 @@ def test_interchange_schema_duckdb() -> None:
assert result == expected
assert df["a"].dtype == nw.Int64
assert df.columns == list(expected.keys())
assert df.collect_schema() == expected


def test_invalid() -> None:
Expand Down
Loading