-
-
Notifications
You must be signed in to change notification settings - Fork 19.5k
ENH: Support arrow/parquet roundtrip for nullable integer / string extension dtypes #29483
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
Changes from 3 commits
4e97e05
27dcbfb
72f4142
62ca041
38218ad
0521875
e593f9e
c317aab
b973eb3
5290afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,34 @@ def construct_array_type(cls): | |
| """ | ||
| return IntegerArray | ||
|
|
||
| def __from_arrow__(self, array): | ||
| """Construct IntegerArray from passed pyarrow Array""" | ||
| import pyarrow | ||
|
|
||
| if isinstance(array, pyarrow.Array): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is this False?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The passed pyarrow values can be either a pyarrow.Array or pyarrow.ChunkedArray. Added a comment for this |
||
| chunks = [array] | ||
| else: | ||
| chunks = array.chunks | ||
|
|
||
| results = [] | ||
| for arr in chunks: | ||
| buflist = arr.buffers() | ||
| data = np.frombuffer(buflist[1], dtype=self.type)[ | ||
| arr.offset : arr.offset + len(arr) | ||
| ] | ||
| bitmask = buflist[0] | ||
| if bitmask is not None: | ||
| mask = pyarrow.BooleanArray.from_buffers( | ||
| pyarrow.bool_(), len(arr), [None, bitmask] | ||
| ) | ||
| mask = np.asarray(mask) | ||
| else: | ||
| mask = np.ones(len(arr), dtype=bool) | ||
| int_arr = IntegerArray(data.copy(), ~mask, copy=False) | ||
| results.append(int_arr) | ||
|
|
||
| return IntegerArray._concat_same_type(results) | ||
|
|
||
|
|
||
| def integer_array(values, dtype=None, copy=False): | ||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,22 @@ def construct_array_type(cls) -> "Type[StringArray]": | |
| def __repr__(self) -> str: | ||
| return "StringDtype" | ||
|
|
||
| def __from_arrow__(self, array): | ||
| """Construct StringArray from passed pyarrow Array""" | ||
| import pyarrow | ||
|
|
||
| if isinstance(array, pyarrow.Array): | ||
| chunks = [array] | ||
| else: | ||
| chunks = array.chunks | ||
|
|
||
| results = [] | ||
| for arr in chunks: | ||
| str_arr = StringArray(np.array(arr)) | ||
|
||
| results.append(str_arr) | ||
|
|
||
| return StringArray._concat_same_type(results) | ||
|
|
||
|
|
||
| class StringArray(PandasArray): | ||
| """ | ||
|
|
||
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.
Does this only preserve integer and string extension types or does it preserve all? I assume the former but somewhat unclear in note
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.
It does preserve it for all dtypes that have implemented the needed protocol methods (
__arrow_array__,__from_arrow__), which right now is integer and string for pandas. And potentially any external EA as well, in case those support those protocols.Tried to clarify this in the docs.