-
-
Notifications
You must be signed in to change notification settings - Fork 18.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): | ||
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? 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)) | ||
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. @TomAugspurger Is there a way to turn off validation when creating a StringArray? (in this case I know the elements will be strings or None) 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. Not at the moment. In skip_validation = isinstance(values, type(self)) I'd be open to a public API for allowing the user / library to state that they have the right values. One slight concern though: right now it's expect that the values are strings or 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. Ah, so actually the validation right now is needed here, even if it is just to convert None to nan? 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. I think the None -> nan replacement happens in in 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. Ah, good catch, I switched to |
||
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.