Repository navigation
GH-51652: [Python] Add NumPy StringDType to Arrow conversion - #51157
Conversation
…bject to that string
…ingDType conversion
|
@jorisvandenbossche gentle ping for a review pass on this |
|
|
jorisvandenbossche
left a comment
There was a problem hiding this comment.
This looks good to me!
One small additional test item:
- Can you add a new case for StringDtype to
test_from_numpy_dtype
| if (dtype_->type_num == NPY_VSTRING && !is_string_or_string_view(type_->id())) { | ||
| return Status::TypeError("Expected an Arrow string type for NumPy StringDType, got ", | ||
| type_->ToString()); |
There was a problem hiding this comment.
Is this needed? (we don't seem to have it here for other types)
If you leave it out, we get a less good error message? (which feels as something to improve in general, so fine to leave this here as is, and consider cleaning this up / generalizing this as another issue)
|
One remaining behaviour question I have, is what to do about NaN, which we explicitly disallow for object dtype when converting to string (or to any type other than float, in general), unless you explicitly specify # converting None to null -> fine
>>> pa.array(np.array(["a", None], dtype=object))
<pyarrow.lib.StringArray object at 0x7f813a761300>
[
"a",
null
]
# converting NaN -> complains about not being a string or null (error msg could be better, though ;))
>>> pa.array(np.array(["a", np.nan], dtype=object))
...
ArrowTypeError: Expected bytes, got a 'float' object
# unless specifying `from_pandas=True`, and then NaN also gets recognized as null
>>> pa.array(np.array(["a", np.nan], dtype=object), from_pandas=True)
<pyarrow.lib.StringArray object at 0x7f813bd98ac0>
[
"a",
null
]So on the one hand, we could argue for consistency that also here NaN should only work if |
|
Thanks @alippai and @ngoldbaum! |
|
Thanks Joris! I'm on vacation this week and am participating in a Quansight all-hands next week but am planning to get to the followups you pointed out in your comments soon. |
|
Thanks for landing this, @ngoldbaum! |
Rationale for this change
NumPy 2.0 added
StringDType, a variable-width UTF-8 string dtype.pa.arraycurrently rejects it withUnsupported numpy type 2056. Now that pyarrow requires NumPy 2.0 the conversion can use the publicNpyString_*C API.This supersedes #50951. The first commit is @alippai's first commit from that PR, unchanged. His later commits added batching and new bulk builder APIs in Arrow C++; those can come later in their own PR with benchmarks, as discussed there. Nothing outside the NumPy conversion code changes here.
What changes are included in this PR?
NumPyDtypeToArrowmapsStringDTypetostring, so it is also the inferred type forpa.array,pa.infer_type,pa.from_numpy_dtypeand lists of such arrays.large_stringandstring_viewcan be requested withtype. Any other type raises aTypeErrornaming the requested type.na_objectis astr. NumPy reports such entries as that string from__getitem__, in ufuncs and comparisons, and in casts, and there is no way to tell them apart from a regular entry holding the same string, so the conversion writes the string too. NaN-like sentinels such asnp.nanandpd.NA,None, and arbitrary objects all become arrow nulls.docs/source/python/numpy.rstdescribing the support and the semantics for converting missing data.If left unfixed, #51156 also becomes possible to trigger from Python by creating a StringDType array with a very large entry.
Are these changes tested?
Yes. Tests cover the three target types, strings crossing NumPy's short, medium and long storage thresholds, non-ASCII input, strided input,
mask,None, NaN and string sentinels, the rejected target type, and list inference. I also ran a threaded test that converts an array while other threads hit NumPy's null-comparison error path on the same array to check the lock ordering.Are there any user-facing changes?
Yes.
pa.arrayand the inference functions acceptStringDTypearrays.AI disclosure: I used an AI model to work on the follow-up commits on top of @alippai's first commit and for code review.
GitHub Issue: [Python] Conversion to/from numpy 2.0+ new StringDType #42018
GitHub Issue: [Python] Conversion from numpy StringDtype to Arrow string #51652