Skip to content

BUG: (1.24.0) np.array(np.nan).astype(str) warns RuntimeWarning: invalid value encountered in cast #22843

@corynezinstitchfix

Description

@corynezinstitchfix

Describe the issue:

Since 1.24.0, np.array(np.nan).astype(str) warns RuntimeWarning: invalid value encountered in cast when I believe it should not.

Reproduce the code example:

import numpy as np
np.array(np.nan).astype(str)

Error message:

/.../test.py:2: RuntimeWarning: invalid value encountered in cast
  np.array(np.nan).astype(str)

NumPy/Python version information:

1.24.0 3.9.10 (main, Jun 28 2022, 22:06:41)
[Clang 13.1.6 (clang-1316.0.21.2.5)]

Context for the issue:

I noticed this because the built-in Pandas functionality to convert a categorical series to a string, pd.Series(["a", "b", "c", "a"], dtype="category").astype(str), has started giving the same warning (I believe from this line of code)

Note to anyone else experiencing the same, it can be worked around by casting to an object first: pd.Series(["a", "b", "c", "a"], dtype="category").astype(object).astype(str)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions