-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement np.asarray() #3598
Implement np.asarray() #3598
Changes from 1 commit
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 |
---|---|---|
|
@@ -2523,3 +2523,34 @@ def impl(a, v): | |
return _np_correlate_core(a, v[::-1], Mode.FULL, 1) | ||
|
||
return impl | ||
|
||
def _is_nonelike(ty): | ||
return (ty is None) or isinstance(ty, types.NoneType) | ||
|
||
@overload(np.asarray) | ||
def np_asarray(a, dtype=None): | ||
impl = None | ||
if isinstance(a, types.Array): | ||
if _is_nonelike(dtype) or a.dtype == dtype.dtype: | ||
def impl(a, dtype=None): | ||
return a | ||
else: | ||
def impl(a, dtype=None): | ||
return a.astype(dtype) | ||
elif isinstance(a, (types.Sequence, types.Tuple)): | ||
# Nested lists cannot be unpacked, therefore only single lists are | ||
# permitted and these conform to Sequence and can be unpacked along on | ||
# the same path as Tuple. | ||
if _is_nonelike(dtype): | ||
def impl(a, dtype=None): | ||
return np.array(a) | ||
else: | ||
def impl(a, dtype=None): | ||
return np.array(a, dtype) | ||
elif isinstance(a, (types.Float, types.Integer, types.Boolean)): | ||
dt_conv = a if _is_nonelike(dtype) else dtype | ||
ty = as_dtype(dt_conv) | ||
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.
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 yes, thanks, that explains some of the weirdness 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 just realized that my explanation is wrong. The cache is per signature. A different 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 problem here is due to this #3612 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. |
||
def impl(a, dtype=None): | ||
return np.array(a, ty) | ||
|
||
return impl |
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.
Replace
types.Float
andtypes.Integer
withtypes.Number
so that complex dtype also work.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.
done.