Skip to content

DOC: np.nan_to_num(..., copy=) uses NumPy 1.x description #29045

Closed
haphaeu/numpy
#1

Description

@aditya0by0

Describe the issue:

In NumPy 2.0>, the stricter semantics for the copy=False keyword result in a ValueError in np.nan_to_num when the input is not already a NumPy array or when a copy is required due to dtype mismatch.

This breaks previously valid and widely used patterns that worked in NumPy 1.x.

Reproduce the code example:

import numpy as np

data = [float("nan"), float("inf"), 1.0]
np.nan_to_num(data, copy=False)  # raises ValueError in NumPy 2.0

Error message:

ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).

Python and NumPy Versions:

  • NumPy version: 2.0.x
  • Python version: 3.10+
  • OS: Any

Runtime Environment:

No response

Context for the issue:

Cause

Internally, nan_to_num uses:

x = np.array(x, subok=True, copy=copy)

In NumPy 2.0, this raises an error if a copy is needed but copy=False was passed.

Proposed Fix

Instead of forcing the user to pre-convert input using np.asarray(data) before passing it to nan_to_num, the function should handle this gracefully by adapting its internal logic.

Proposed replacement:

if copy is False:
    x = np.asarray(x)
else:
    x = np.array(x, subok=True, copy=copy)

This preserves the original intent (copy if needed) and avoids unnecessary burden on users to refactor existing code.

Additional Context

The migration guide recommends replacing np.array(..., copy=False) with np.asarray(...) when the goal is "copy if needed". This change would make nan_to_num robust across versions and eliminate the need for callers to pre-process data explicitly.

Activity

  1. added
    33 - Question
    and removed
    33 - Question
    on May 25, 2025
  2. changed the title [-]BUG: `np.nan_to_num(..., copy=False)` raises `ValueError` in NumPy 2.0 when array creation requires copy[/-] [+]DOC: `np.nan_to_num(..., copy=)` uses NumPy 1.x description[/+] on May 25, 2025
  3. seberg commented on May 25, 2025

    @seberg
    Member

    You should just use copy=None and the documentation should be changed. It's best to keep the meaning of copy= aligned with the rest of NumPy even if copy=None is a bit awkward, I would say.
    So this is just a doc change, because the docs read a bit like copy was different here.

  4. akshitasure12 commented on May 28, 2025

    @akshitasure12

    I'm a new contributor to NumPy and came across this issue - Hope it still needs to be addressed!

  5. added a commit that references this issue on Jun 19, 2025
    a6329e6
  6. added a commit that references this issue on Sep 11, 2026
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions