|
47 | 47 | Time, |
48 | 48 | Utf8, |
49 | 49 | py_type_to_dtype, |
50 | | - unpack_dtypes, |
51 | 50 | ) |
52 | 51 | from polars.dependencies import ( |
53 | 52 | _PYARROW_AVAILABLE, |
|
112 | 111 | with contextlib.suppress(ImportError): # Module not available when building docs |
113 | 112 | from polars.polars import PyDataFrame |
114 | 113 |
|
115 | | - |
116 | 114 | if TYPE_CHECKING: |
117 | 115 | import sys |
118 | 116 | from datetime import timedelta |
119 | 117 | from io import IOBase |
120 | 118 | from typing import Literal |
121 | 119 |
|
122 | 120 | import deltalake |
123 | | - from pyarrow.interchange.dataframe import _PyArrowDataFrame |
124 | 121 | from xlsxwriter import Workbook |
125 | 122 |
|
126 | 123 | from polars import Expr, LazyFrame, Series |
| 124 | + from polars.interchange.dataframe import PolarsDataFrame |
127 | 125 | from polars.type_aliases import ( |
128 | 126 | AsofJoinStrategy, |
129 | 127 | AvroCompression, |
@@ -1208,48 +1206,51 @@ def __array__(self, dtype: Any = None) -> np.ndarray[Any, Any]: |
1208 | 1206 |
|
1209 | 1207 | def __dataframe__( |
1210 | 1208 | self, nan_as_null: bool = False, allow_copy: bool = True |
1211 | | - ) -> _PyArrowDataFrame: |
| 1209 | + ) -> PolarsDataFrame: |
1212 | 1210 | """ |
1213 | 1211 | Convert to a dataframe object implementing the dataframe interchange protocol. |
1214 | 1212 |
|
1215 | 1213 | Parameters |
1216 | 1214 | ---------- |
1217 | 1215 | nan_as_null |
1218 | 1216 | Overwrite null values in the data with ``NaN``. |
| 1217 | +
|
| 1218 | + .. warning:: |
| 1219 | + This functionality has not been implemented and the parameter will be |
| 1220 | + removed in a future version. |
| 1221 | + Setting this to ``True`` will raise a ``NotImplementedError``. |
1219 | 1222 | allow_copy |
1220 | | - Allow memory to be copied to perform the conversion. If set to False, causes |
1221 | | - conversions that are not zero-copy to fail. |
| 1223 | + Allow memory to be copied to perform the conversion. If set to ``False``, |
| 1224 | + causes conversions that are not zero-copy to fail. |
1222 | 1225 |
|
1223 | 1226 | Notes |
1224 | 1227 | ----- |
1225 | | - Details on the dataframe interchange protocol: |
| 1228 | + Details on the Python dataframe interchange protocol: |
1226 | 1229 | https://data-apis.org/dataframe-protocol/latest/index.html |
1227 | 1230 |
|
1228 | | - ``nan_as_null`` currently has no effect; once support for nullable extension |
1229 | | - dtypes is added, this value should be propagated to columns. |
1230 | | -
|
1231 | | - Polars currently relies on pyarrow's implementation of the dataframe interchange |
1232 | | - protocol. Therefore, pyarrow>=11.0.0 is required for this method to work. |
| 1231 | + Examples |
| 1232 | + -------- |
| 1233 | + Convert a Polars dataframe to a generic dataframe object and access some |
| 1234 | + properties. |
1233 | 1235 |
|
1234 | | - Because Polars can not currently guarantee zero-copy conversion to Arrow for |
1235 | | - categorical columns, ``allow_copy=False`` will not work if the dataframe |
1236 | | - contains categorical data. |
| 1236 | + >>> df = pl.DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["x", "y"]}) |
| 1237 | + >>> dfi = df.__dataframe__() |
| 1238 | + >>> dfi.num_rows() |
| 1239 | + 2 |
| 1240 | + >>> dfi.get_column(1).dtype |
| 1241 | + (<DtypeKind.FLOAT: 2>, 64, 'g', '=') |
1237 | 1242 |
|
1238 | 1243 | """ |
1239 | | - if not _PYARROW_AVAILABLE or parse_version(pa.__version__) < parse_version( |
1240 | | - "11" |
1241 | | - ): |
1242 | | - raise ImportError( |
1243 | | - "pyarrow>=11.0.0 is required for converting a Polars dataframe to a" |
1244 | | - " dataframe interchange object." |
1245 | | - ) |
1246 | | - if not allow_copy and Categorical in unpack_dtypes(*self.dtypes): |
1247 | | - raise TypeError( |
1248 | | - "Polars can not currently guarantee zero-copy conversion to Arrow for" |
1249 | | - " categorical columns. Set `allow_copy=True` or cast categorical" |
1250 | | - " columns to string first." |
| 1244 | + if nan_as_null: |
| 1245 | + raise NotImplementedError( |
| 1246 | + "functionality for `nan_as_null` has not been implemented and the" |
| 1247 | + " parameter will be removed in a future version." |
| 1248 | + " Use the default `nan_as_null=False`." |
1251 | 1249 | ) |
1252 | | - return self.to_arrow().__dataframe__(nan_as_null, allow_copy) |
| 1250 | + |
| 1251 | + from polars.interchange.dataframe import PolarsDataFrame |
| 1252 | + |
| 1253 | + return PolarsDataFrame(self, allow_copy=allow_copy) |
1253 | 1254 |
|
1254 | 1255 | def __dataframe_consortium_standard__( |
1255 | 1256 | self, *, api_version: str | None = None |
|
0 commit comments