-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Create ArrowDtype #46774
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
ENH: Create ArrowDtype #46774
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
238a44e
ENH: Create BaseArrowDtype & NumericArrowDtype
mroeschke 95cbcbc
Merge remote-tracking branch 'upstream/main' into ci/targeted_pyarrow
mroeschke f681d5e
Simplfy inheritance
mroeschke 9fe6cac
Merge remote-tracking branch 'upstream/main' into ci/targeted_pyarrow
mroeschke a5cafa1
Merge remote-tracking branch 'upstream/main' into ci/targeted_pyarrow
mroeschke 3fbbdba
add construct from string
mroeschke e4aa49c
Rename
mroeschke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
import pyarrow as pa | ||
|
||
from pandas._typing import DtypeObj | ||
from pandas.util._decorators import cache_readonly | ||
|
||
from pandas.core.dtypes.base import StorageExtensionDtype | ||
|
||
from pandas.core.arrays.arrow import ArrowExtensionArray | ||
|
||
|
||
class ArrowDtype(StorageExtensionDtype): | ||
""" | ||
Base class for dtypes for BaseArrowArray subclasses. | ||
Modeled after BaseMaskedDtype | ||
""" | ||
|
||
name: str | ||
base = None | ||
type: pa.DataType | ||
|
||
na_value = pa.NA | ||
|
||
def __init__(self, storage="pyarrow") -> None: | ||
super().__init__(storage) | ||
|
||
@cache_readonly | ||
def numpy_dtype(self) -> np.dtype: | ||
"""Return an instance of the related numpy dtype""" | ||
return self.type.to_pandas_dtype() | ||
|
||
@cache_readonly | ||
def kind(self) -> str: | ||
return self.numpy_dtype.kind | ||
|
||
@cache_readonly | ||
def itemsize(self) -> int: | ||
"""Return the number of bytes in this dtype""" | ||
return self.numpy_dtype.itemsize | ||
|
||
@classmethod | ||
def construct_array_type(cls): | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
Returns | ||
------- | ||
type | ||
""" | ||
return ArrowExtensionArray | ||
|
||
@classmethod | ||
def from_numpy_dtype(cls, dtype: np.dtype) -> ArrowDtype: | ||
""" | ||
Construct the ArrowDtype corresponding to the given numpy dtype. | ||
""" | ||
# TODO: This may be incomplete | ||
pa_dtype = pa.from_numpy_dtype(dtype) | ||
if pa_dtype is cls.type: | ||
return cls() | ||
raise NotImplementedError(dtype) | ||
|
||
def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None: | ||
# We unwrap any masked dtypes, find the common dtype we would use | ||
# for that, then re-mask the result. | ||
from pandas.core.dtypes.cast import find_common_type | ||
|
||
new_dtype = find_common_type( | ||
[ | ||
dtype.numpy_dtype if isinstance(dtype, ArrowDtype) else dtype | ||
for dtype in dtypes | ||
] | ||
) | ||
if not isinstance(new_dtype, np.dtype): | ||
# If we ever support e.g. Masked[DatetimeArray] then this will change | ||
return None | ||
try: | ||
return type(self).from_numpy_dtype(new_dtype) | ||
except (KeyError, NotImplementedError): | ||
return None | ||
|
||
def __from_arrow__(self, array: pa.Array | pa.ChunkedArray): | ||
""" | ||
Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray. | ||
""" | ||
array_class = self.construct_array_type() | ||
return array_class(array) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are the dtypes here? shouldn't these be in pandas/core/dtypes ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of consolidating all arrow related functionality under
/arrays/arrow/
. Thoughts @jbrockmendelThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no objection. Might be worth keeping something in core.dtypes.dtypes for dependency-structure purposes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok fair, looking to keep these separate as much as possible, maybe rename this to _dtype.py at some point.