From ea25916acce8320260c8bfb14576195027d78cf3 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Wed, 2 Aug 2023 11:16:20 +0100 Subject: [PATCH] feat(python): add entry point to the Consortium DataFrame API --- py-polars/polars/dataframe/frame.py | 20 ++++++++++++++++++++ py-polars/polars/lazyframe/frame.py | 20 ++++++++++++++++++++ py-polars/polars/series/series.py | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index a4c10081ae4a..7b938f58c6f6 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -1235,6 +1235,26 @@ def __dataframe__( ) return self.to_arrow().__dataframe__(nan_as_null, allow_copy) + def __dataframe_consortium_standard__( + self, /, *, api_version: str | None = None + ) -> Any: + """ + Provide entry point to the Consortium DataFrame Standard API. + + This is developed and maintained outside of polars. + Please report any issues to https://github.com/data-apis/dataframe-api-compat. + """ + try: + from dataframe_api_compat import polars_standard # type: ignore[import] + except ModuleNotFoundError: + raise ImportError( + "`dataframe-api-compat` package is required for using the " + "Consortium DataFrame Standard API." + ) from None + return polars_standard.convert_to_standard_compliant_dataframe( + self, api_version=api_version + ) + def _comp(self, other: Any, op: ComparisonOperator) -> DataFrame: """Compare a DataFrame with another object.""" if isinstance(other, DataFrame): diff --git a/py-polars/polars/lazyframe/frame.py b/py-polars/polars/lazyframe/frame.py index eec91f204c6f..350f3c02d08c 100644 --- a/py-polars/polars/lazyframe/frame.py +++ b/py-polars/polars/lazyframe/frame.py @@ -622,6 +622,26 @@ def schema(self) -> SchemaDict: """ return self._ldf.schema() + def __dataframe_consortium_standard__( + self, /, *, api_version: str | None = None + ) -> Any: + """ + Provide entry point to the Consortium DataFrame Standard API. + + This is developed and maintained outside of polars. + Please report any issues to https://github.com/data-apis/dataframe-api-compat. + """ + try: + from dataframe_api_compat import polars_standard # type: ignore[import] + except ModuleNotFoundError: + raise ImportError( + "`dataframe-api-compat` package is required for using the " + "Consortium DataFrame Standard API." + ) from None + return polars_standard.convert_to_standard_compliant_dataframe( + self, api_version=api_version + ) + @property def width(self) -> int: """ diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 67c5c6160331..1d4dedc84b2a 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -1119,6 +1119,26 @@ def __array_ufunc__( f" `{method}`." ) + def __column_consortium_standard__( + self, /, *, api_version: str | None = None + ) -> Any: + """ + Provide entry point to the Consortium DataFrame Standard API. + + This is developed and maintained outside of polars. + Please report any issues to https://github.com/data-apis/dataframe-api-compat. + """ + try: + from dataframe_api_compat import polars_standard # type: ignore[import] + except ModuleNotFoundError: + raise ImportError( + "`dataframe-api-compat` package is required for using the " + "Consortium DataFrame Standard API." + ) from None + return polars_standard.convert_to_standard_compliant_column( + self, api_version=api_version + ) + def _repr_html_(self) -> str: """Format output data in HTML for display in Jupyter Notebooks.""" return self.to_frame()._repr_html_(from_series=True)