Skip to content

Commit abe511c

Browse files
authored
feat: support dataframe.columns for interchange level (#1285)
1 parent 756d0e2 commit abe511c

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

docs/extending.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ libraries which have interchange only support can access the following methods a
6969
converting to Narwhals DataFrame:
7070

7171
- `.schema`, hence column names via `.schema.names()` and column types via `.schema.dtypes()`
72+
- `.columns`
7273
- `.to_pandas()` and `.to_arrow()`, for converting to Pandas and Arrow, respectively.
7374
- `.select(names)` (Ibis and DuckDB), where `names` is a list of (string) column names. This is useful for
7475
selecting columns before converting to another library.

narwhals/_duckdb/dataframe.py

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def __getattr__(self, attr: str) -> Any:
116116
self._native_frame.columns, self._native_frame.types
117117
)
118118
}
119+
elif attr == "columns":
120+
return self._native_frame.columns
119121

120122
msg = ( # pragma: no cover
121123
f"Attribute {attr} is not supported for metadata-only dataframes.\n\n"

narwhals/_ibis/dataframe.py

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def __getattr__(self, attr: str) -> Any:
109109
column_name: map_ibis_dtype_to_narwhals_dtype(ibis_dtype, self._dtypes)
110110
for column_name, ibis_dtype in self._native_frame.schema().items()
111111
}
112+
elif attr == "columns":
113+
return self._native_frame.columns
112114
msg = (
113115
f"Attribute {attr} is not supported for metadata-only dataframes.\n\n"
114116
"If you would like to see this kind of object better supported in "

tests/frame/interchange_schema_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def test_interchange_schema_ibis(
156156
}
157157
assert result == expected
158158
assert df["a"].dtype == nw.Int64
159+
assert df.columns == list(expected.keys())
159160

160161

161162
def test_interchange_schema_duckdb() -> None:
@@ -220,6 +221,7 @@ def test_interchange_schema_duckdb() -> None:
220221
}
221222
assert result == expected
222223
assert df["a"].dtype == nw.Int64
224+
assert df.columns == list(expected.keys())
223225

224226

225227
def test_invalid() -> None:

0 commit comments

Comments
 (0)