Skip to content

Commit

Permalink
feat: support dataframe.columns for interchange level (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Oct 29, 2024
1 parent 756d0e2 commit abe511c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ libraries which have interchange only support can access the following methods a
converting to Narwhals DataFrame:

- `.schema`, hence column names via `.schema.names()` and column types via `.schema.dtypes()`
- `.columns`
- `.to_pandas()` and `.to_arrow()`, for converting to Pandas and Arrow, respectively.
- `.select(names)` (Ibis and DuckDB), where `names` is a list of (string) column names. This is useful for
selecting columns before converting to another library.
Expand Down
2 changes: 2 additions & 0 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def __getattr__(self, attr: str) -> Any:
self._native_frame.columns, self._native_frame.types
)
}
elif attr == "columns":
return self._native_frame.columns

msg = ( # pragma: no cover
f"Attribute {attr} is not supported for metadata-only dataframes.\n\n"
Expand Down
2 changes: 2 additions & 0 deletions narwhals/_ibis/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def __getattr__(self, attr: str) -> Any:
column_name: map_ibis_dtype_to_narwhals_dtype(ibis_dtype, self._dtypes)
for column_name, ibis_dtype in self._native_frame.schema().items()
}
elif attr == "columns":
return self._native_frame.columns
msg = (
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 "
Expand Down
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())


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


def test_invalid() -> None:
Expand Down

0 comments on commit abe511c

Please sign in to comment.