Skip to content
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

np.nan_to_num ValueError: ValueError: Unable to avoid copy #272

Open
scottyhq opened this issue Jun 21, 2024 · 1 comment
Open

np.nan_to_num ValueError: ValueError: Unable to avoid copy #272

scottyhq opened this issue Jun 21, 2024 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@scottyhq
Copy link
Contributor

scottyhq commented Jun 21, 2024

https://tutorial.xarray.dev/fundamentals/03.1_computation_with_xarray.html#applying-arbitrary-functions

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 np.nan_to_num(ds.sst, 0)

File ~/miniforge3/envs/xarray-tutorial-v1/lib/python3.12/site-packages/numpy/lib/_type_check_impl.py:460, in nan_to_num(x, copy, nan, posinf, neginf)
    366 @array_function_dispatch(_nan_to_num_dispatcher)
    367 def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
    368     """
    369     Replace NaN with zero and infinity with large finite numbers (default
    370     behaviour) or with the numbers defined by the user using the `nan`,
   (...)
    458     array([222222.+111111.j, 111111.     +0.j, 111111.+222222.j])
    459     """
--> 460     x = _nx.array(x, subok=True, copy=copy)
    461     xtype = x.dtype.type
    463     isscalar = (x.ndim == 0)

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).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

Seen upgrading package versions (#270) if numpy>=2 in environment

@scottyhq scottyhq added the bug Something isn't working label Jun 21, 2024
@scottyhq
Copy link
Contributor Author

Looks like the tutorial code should just use named keywords instead of positional

np.nan_to_num(ds.sst, 0) is actually intended to be np.nan_to_num(ds.sst, nan=0) but according to the docstring order is actually setting copy=0 (copy=False):

np.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)

@scottyhq scottyhq added the good first issue Good for newcomers label Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant