You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a follow-up to #32040 (comment), where I suggested that once we start treating str properly as a scalar for StringDType, we might be able to do some of the handling of it using _array_converter. It needs a small change to _array_converter (hence ping @seberg), but with it the code in strings.py does become somewhat simpler (especially for the T case).
Note that in principle it would be nice to have np.result_type(array, "string") work, just as is the case for np.result_type(array, 1.0). However, strings are already greedily interpreted as dtypes, so that doesn't work (goes to show that "conveniences" are not always a good idea). But _array_converter does not have that problem.
Opening as draft for now, since I'm not 100% sure this is the best approach (esp. for _array_converter, but also just in ensuring consistency between different numpy parts).
Note that in principle it would be nice to have np.result_type(array, "string") work, just as is the case for np.result_type(array, 1.0)
Hmmm, makes me wonder if we should consider something like result_type(*, dtypes=None, values=None). One huge problem with result_type after all is that it coerces to dtypes and kwargs could clean that up in theory.
But yeah, making this helper deal with it makes more sense anyway probably, as it can more easily avoid the conversion to an array with wrong dtype detour.
(I am not immediately sure how dtype discovery works here, did the array-converter actually skip converting to a full array for Ptyhon scalars? As it still needs to discover the string length, unfortunately.)
Note that in principle it would be nice to have np.result_type(array, "string") work, just as is the case for np.result_type(array, 1.0)
Hmmm, makes me wonder if we should consider something like result_type(*, dtypes=None, values=None). One huge problem with result_type after all is that it coerces to dtypes and kwargs could clean that up in theory.
I like that idea.
But yeah, making this helper deal with it makes more sense anyway probably, as it can more easily avoid the conversion to an array with wrong dtype detour.
Yes, and also do the conversion to with asanyarray that is wanted anyway.
(I am not immediately sure how dtype discovery works here, did the array-converter actually skip converting to a full array for Ptyhon scalars? As it still needs to discover the string length, unfortunately.)
Hmm, I should check, and if there aren't some already, add some test cases that ensure it remains consistent.
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
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.
This is a follow-up to #32040 (comment), where I suggested that once we start treating
strproperly as a scalar forStringDType, we might be able to do some of the handling of it using_array_converter. It needs a small change to_array_converter(hence ping @seberg), but with it the code instrings.pydoes become somewhat simpler (especially for theTcase).Note that in principle it would be nice to have
np.result_type(array, "string")work, just as is the case fornp.result_type(array, 1.0). However, strings are already greedily interpreted as dtypes, so that doesn't work (goes to show that "conveniences" are not always a good idea). But_array_converterdoes not have that problem.Opening as draft for now, since I'm not 100% sure this is the best approach (esp. for
_array_converter, but also just in ensuring consistency between different numpy parts).No AI.