Thanks to visit codestin.com
Credit goes to github.com

Skip to content

"Controlled" creation of object arrays #15047

Closed
@anntzer

Description

@anntzer

Auto-creation of object arrays was recently deprecated in numpy. I agree with the change, but it seems a bit hard to write certain kinds of generic code that determine whether a user-provided argument is convertible to a non-object array.

Reproducing code example:

Matplotlib contains the following snippet:

    # <named ("string") colors are handled earlier>
    # tuple color.
    c = np.array(c)
    if not np.can_cast(c.dtype, float, "same_kind") or c.ndim != 1:
        # Test the dtype explicitly as `map(float, ...)`, `np.array(...,
        # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
        # Test dimensionality to reject single floats.
        raise ValueError(f"Invalid RGBA argument: {orig_c!r}")

but sometimes the function is called with an array of colors in various formats (e.g. ["red", (0.5, 0.5, 0.5), "blue"]) -- we catch the ValueError and convert each item one at a time instead.

Now the call to np.array(c) will emit a DeprecationWarning. How can we work around that? Even something like np.min_scalar_type(c) emits a warning (which I would guess it shouldn't?), so it's not obvious to me how to check "if we converted this thing to an array, what would the dtype be?"

Numpy/Python version information:

1.19.0.dev0+bd1adc3 3.8.0 (default, Nov 6 2019, 21:49:08)
[GCC 7.3.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    57 - Close?Issues which may be closable unless discussion continued

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions