np.nan_to_num ValueError: ValueError: Unable to avoid copy
scottyhq opened this issue · 1 comments
scottyhq commented
---------------------------------------------------------------------------
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 commented
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)