-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(python): Native implementation of dataframe interchange protocol #10267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7510bc2
Move from_dataframe
stinodego cb24709
Implement interchange protocol natively
stinodego e2c9ebe
Add tests
stinodego f4a8890
Add a note about slicing in `get_chunks`
stinodego 3d1a2f5
Minor optimization for dtype conversion calls
stinodego 7abb47b
Improve dtype conversion performance
stinodego dee3f12
Fix return type of base_type util
stinodego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| """ | ||
| Module containing the implementation of the Python dataframe interchange protocol. | ||
| Details on the protocol: | ||
| https://data-apis.org/dataframe-protocol/latest/index.html | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from polars.interchange.protocol import DlpackDeviceType, DtypeKind | ||
| from polars.interchange.utils import polars_dtype_to_dtype | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import NoReturn | ||
|
|
||
| from polars import Series | ||
|
|
||
|
|
||
| class PolarsBuffer: | ||
| """ | ||
| A buffer object backed by a Polars Series consisting of a single chunk. | ||
| Parameters | ||
| ---------- | ||
| data | ||
| The Polars Series backing the buffer object. | ||
| allow_copy | ||
| Allow data to be copied during operations on this column. If set to ``False``, | ||
| a RuntimeError will be raised if data would be copied. | ||
| """ | ||
|
|
||
| def __init__(self, data: Series, *, allow_copy: bool = True): | ||
| if data.n_chunks() > 1: | ||
| if not allow_copy: | ||
| raise RuntimeError( | ||
| "non-contiguous buffer must be made contiguous, which is not zero-copy" | ||
| ) | ||
| data = data.rechunk() | ||
|
|
||
| self._data = data | ||
|
|
||
| @property | ||
| def bufsize(self) -> int: | ||
| """Buffer size in bytes.""" | ||
| dtype = polars_dtype_to_dtype(self._data.dtype) | ||
|
|
||
| if dtype[0] == DtypeKind.STRING: | ||
| return self._data.str.lengths().sum() # type: ignore[return-value] | ||
|
|
||
| n_bits = self._data.len() * dtype[1] | ||
|
|
||
| result, rest = divmod(n_bits, 8) | ||
| # Round up to the nearest byte | ||
| if rest: | ||
| return result + 1 | ||
| else: | ||
| return result | ||
|
|
||
| @property | ||
| def ptr(self) -> int: | ||
| """Pointer to start of the buffer as an integer.""" | ||
| _offset, _length, pointer = self._data._s.get_ptr() | ||
| return pointer | ||
|
|
||
| def __dlpack__(self) -> NoReturn: | ||
| """Represent this structure as DLPack interface.""" | ||
| raise NotImplementedError("__dlpack__") | ||
|
|
||
| def __dlpack_device__(self) -> tuple[DlpackDeviceType, None]: | ||
| """Device type and device ID for where the data in the buffer resides.""" | ||
| return (DlpackDeviceType.CPU, None) | ||
|
|
||
| def __repr__(self) -> str: | ||
| bufsize = self.bufsize | ||
| ptr = self.ptr | ||
| device = self.__dlpack_device__()[0].name | ||
| return f"PolarsBuffer(bufsize={bufsize}, ptr={ptr}, device={device!r})" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.