Skip to content

Cross-kind casting in asarray() #264

Closed

Description

@asmeurer

Should asarray() allow arbitrary casting between dtypes if you call asarray(x, dtype=dtype)? It isn't really clear in the spec if, e.g., asarray(x, dtype=bool) should be required to work if x is an integer or float array, or asarray(x, dtype=float64) if x is integer, and so on. My feeling is that it definitely should work if dtype is a promotable type from x.dtype, but I don't know if casts that aren't required to promote should also be required to work.

Activity

  1. rgommers commented on Sep 14, 2021

    @rgommers
    Member

    I'd expect this to work, because it's explicit. Unless there's an obvious alternative, like x.astype() (which we don't have).

  2. asmeurer commented on Sep 17, 2021

    @asmeurer
    MemberAuthor

    This is also relevant for the *_like creation functions, which all accept a dtype parameter. https://data-apis.org/array-api/latest/API_specification/creation_functions.html

  3. asmeurer commented on Sep 17, 2021

    @asmeurer
    MemberAuthor

    I guess there's actually three levels of dtype casting:

    • Promotable casting (x.dtype can be promoted to dtype in the type promotion table
    • Same-kind casting (x.dtype is the same kind as dtype but may be a downcast, like int64 -> int8 or uint64 -> int64)
    • Cross-kind casting (x.dtype and dtype are different kinds (integer, floating-point, boolean, eventually complex)).

    Presumably at the very least promotion should be allowed, otherwise the dtype parameter is useless, especially for the *_like functions. The other two involve losing information for some values (except for bool -> other).

  4. kgryte commented on Sep 18, 2021

    @kgryte
    Contributor

    @asmeurer I am not seeing how the *_like functions are affected by cross-kind casting, as discussed in this issue. Maybe I am missing something, but the *_like functions don't perform casting; the data of the input array is not touched.

  5. asmeurer commented on Sep 20, 2021

    @asmeurer
    MemberAuthor

    Oh yeah you are right about the _like functions (those always confuse me tbh because they seem rather pointless, but that's neither here nor there).

  6. added this to the v2021 milestone on Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions