Skip to content
forked from pydata/xarray

Commit

Permalink
switch the documentation to run with numpy>=2 (pydata#9177)
Browse files Browse the repository at this point in the history
* `NaN` → `nan`

* new numpy scalar repr

* unpin `numpy` in the docs [skip-ci]

* try extracting the scalars to pass to `linspace`

* use `numpy` instead of `numpy.array_api` [skip-ci]

and mention that we're falling back to `numpy.array_api` in `numpy<2.0`

* Update ci/requirements/doc.yml

* Remove 2D cross product doctests

* One more fix

---------

Co-authored-by: Jessica Scheick <[email protected]>
Co-authored-by: Deepak Cherian <[email protected]>
Co-authored-by: Deepak Cherian <[email protected]>
  • Loading branch information
4 people authored Jul 11, 2024
1 parent e12aa44 commit c1965c2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 60 deletions.
2 changes: 1 addition & 1 deletion ci/requirements/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
- nbsphinx
- netcdf4>=1.5
- numba
- numpy>=1.21,<2
- numpy>=2
- packaging>=21.3
- pandas>=1.4,!=2.1.0
- pooch
Expand Down
4 changes: 2 additions & 2 deletions doc/getting-started-guide/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ Some packages may have additional functionality beyond what is shown here. You c
How does xarray handle missing values?
--------------------------------------

**xarray can handle missing values using ``np.NaN``**
**xarray can handle missing values using ``np.nan``**

- ``np.NaN`` is used to represent missing values in labeled arrays and datasets. It is a commonly used standard for representing missing or undefined numerical data in scientific computing. ``np.NaN`` is a constant value in NumPy that represents "Not a Number" or missing values.
- ``np.nan`` is used to represent missing values in labeled arrays and datasets. It is a commonly used standard for representing missing or undefined numerical data in scientific computing. ``np.nan`` is a constant value in NumPy that represents "Not a Number" or missing values.

- Most of xarray's computation methods are designed to automatically handle missing values appropriately.

Expand Down
4 changes: 2 additions & 2 deletions doc/user-guide/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ However, the functions also take missing values in the data into account:

.. ipython:: python
data = xr.DataArray([np.NaN, 2, 4])
data = xr.DataArray([np.nan, 2, 4])
weights = xr.DataArray([8, 1, 1])
data.weighted(weights).mean()
Expand All @@ -444,7 +444,7 @@ If the weights add up to to 0, ``sum`` returns 0:
data.weighted(weights).sum()
and ``mean``, ``std`` and ``var`` return ``NaN``:
and ``mean``, ``std`` and ``var`` return ``nan``:

.. ipython:: python
Expand Down
4 changes: 2 additions & 2 deletions doc/user-guide/interpolation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ Let's see how :py:meth:`~xarray.DataArray.interp` works on real data.
axes[0].set_title("Raw data")
# Interpolated data
new_lon = np.linspace(ds.lon[0], ds.lon[-1], ds.sizes["lon"] * 4)
new_lat = np.linspace(ds.lat[0], ds.lat[-1], ds.sizes["lat"] * 4)
new_lon = np.linspace(ds.lon[0].item(), ds.lon[-1].item(), ds.sizes["lon"] * 4)
new_lat = np.linspace(ds.lat[0].item(), ds.lat[-1].item(), ds.sizes["lat"] * 4)
dsi = ds.interp(lat=new_lat, lon=new_lon)
dsi.air.plot(ax=axes[1])
@savefig interpolation_sample3.png width=8in
Expand Down
5 changes: 3 additions & 2 deletions doc/user-guide/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ If the array type you want to generate has an array API-compliant top-level name
you can use this neat trick:

.. ipython:: python
:okwarning:
from numpy import array_api as xp # available in numpy 1.26.0
import numpy as xp # compatible in numpy 2.0
# use `import numpy.array_api as xp` in numpy>=1.23,<2.0
from hypothesis.extra.array_api import make_strategies_namespace
Expand Down
47 changes: 1 addition & 46 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ def apply_ufunc(
supported:
>>> magnitude(3, 4)
5.0
np.float64(5.0)
>>> magnitude(3, np.array([0, 4]))
array([3., 5.])
>>> magnitude(array, 0)
Expand Down Expand Up @@ -1587,15 +1587,6 @@ def cross(
array([-3, 6, -3])
Dimensions without coordinates: dim_0
Vector cross-product with 2 dimensions, returns in the perpendicular
direction:
>>> a = xr.DataArray([1, 2])
>>> b = xr.DataArray([4, 5])
>>> xr.cross(a, b, dim="dim_0")
<xarray.DataArray ()> Size: 8B
array(-3)
Vector cross-product with 3 dimensions but zeros at the last axis
yields the same results as with 2 dimensions:
Expand All @@ -1606,42 +1597,6 @@ def cross(
array([ 0, 0, -3])
Dimensions without coordinates: dim_0
One vector with dimension 2:
>>> a = xr.DataArray(
... [1, 2],
... dims=["cartesian"],
... coords=dict(cartesian=(["cartesian"], ["x", "y"])),
... )
>>> b = xr.DataArray(
... [4, 5, 6],
... dims=["cartesian"],
... coords=dict(cartesian=(["cartesian"], ["x", "y", "z"])),
... )
>>> xr.cross(a, b, dim="cartesian")
<xarray.DataArray (cartesian: 3)> Size: 24B
array([12, -6, -3])
Coordinates:
* cartesian (cartesian) <U1 12B 'x' 'y' 'z'
One vector with dimension 2 but coords in other positions:
>>> a = xr.DataArray(
... [1, 2],
... dims=["cartesian"],
... coords=dict(cartesian=(["cartesian"], ["x", "z"])),
... )
>>> b = xr.DataArray(
... [4, 5, 6],
... dims=["cartesian"],
... coords=dict(cartesian=(["cartesian"], ["x", "y", "z"])),
... )
>>> xr.cross(a, b, dim="cartesian")
<xarray.DataArray (cartesian: 3)> Size: 24B
array([-10, 2, 5])
Coordinates:
* cartesian (cartesian) <U1 12B 'x' 'y' 'z'
Multiple vector cross-products. Note that the direction of the
cross product vector is defined by the right-hand rule:
Expand Down
4 changes: 2 additions & 2 deletions xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def _get_largest_lims(self) -> dict[str, tuple[float, float]]:
>>> ds = xr.tutorial.scatter_example_dataset(seed=42)
>>> fg = ds.plot.scatter(x="A", y="B", hue="y", row="x", col="w")
>>> round(fg._get_largest_lims()["x"][0], 3)
-0.334
np.float64(-0.334)
"""
lims_largest: dict[str, tuple[float, float]] = dict(
x=(np.inf, -np.inf), y=(np.inf, -np.inf), z=(np.inf, -np.inf)
Expand Down Expand Up @@ -817,7 +817,7 @@ def _set_lims(
>>> fg = ds.plot.scatter(x="A", y="B", hue="y", row="x", col="w")
>>> fg._set_lims(x=(-0.3, 0.3), y=(0, 2), z=(0, 4))
>>> fg.axs[0, 0].get_xlim(), fg.axs[0, 0].get_ylim()
((-0.3, 0.3), (0.0, 2.0))
((np.float64(-0.3), np.float64(0.3)), (np.float64(0.0), np.float64(2.0)))
"""
lims_largest = self._get_largest_lims()

Expand Down
6 changes: 3 additions & 3 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,11 @@ def _update_axes(
def _is_monotonic(coord, axis=0):
"""
>>> _is_monotonic(np.array([0, 1, 2]))
True
np.True_
>>> _is_monotonic(np.array([2, 1, 0]))
True
np.True_
>>> _is_monotonic(np.array([0, 2, 1]))
False
np.False_
"""
if coord.shape[axis] < 3:
return True
Expand Down

0 comments on commit c1965c2

Please sign in to comment.