Skip to content

Commit

Permalink
feat(python): add entry point to the Consortium DataFrame API
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Aug 2, 2023
1 parent 2a6fa38 commit ea25916
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 20 additions & 0 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
20 changes: 20 additions & 0 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ea25916

Please sign in to comment.