GH-42018: [Python] Add NumPy StringDType to Arrow conversion - #51157
Open
ngoldbaum wants to merge 7 commits into
Open
GH-42018: [Python] Add NumPy StringDType to Arrow conversion#51157ngoldbaum wants to merge 7 commits into
ngoldbaum wants to merge 7 commits into
Conversation
…bject to that string
…ingDType conversion
ngoldbaum
requested review from
AlenkaF,
pitrou,
raulcd and
rok
as code owners
September 3, 2026 19:50
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.