Skip to content

Commit c1965c2

Browse files
keewisJessicaS11dcherian
authored
switch the documentation to run with numpy>=2 (#9177)
* `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]>
1 parent e12aa44 commit c1965c2

File tree

8 files changed

+16
-60
lines changed

8 files changed

+16
-60
lines changed

ci/requirements/doc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies:
2121
- nbsphinx
2222
- netcdf4>=1.5
2323
- numba
24-
- numpy>=1.21,<2
24+
- numpy>=2
2525
- packaging>=21.3
2626
- pandas>=1.4,!=2.1.0
2727
- pooch

doc/getting-started-guide/faq.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ Some packages may have additional functionality beyond what is shown here. You c
352352
How does xarray handle missing values?
353353
--------------------------------------
354354

355-
**xarray can handle missing values using ``np.NaN``**
355+
**xarray can handle missing values using ``np.nan``**
356356

357-
- ``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.
357+
- ``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.
358358

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

doc/user-guide/computation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ However, the functions also take missing values in the data into account:
426426

427427
.. ipython:: python
428428
429-
data = xr.DataArray([np.NaN, 2, 4])
429+
data = xr.DataArray([np.nan, 2, 4])
430430
weights = xr.DataArray([8, 1, 1])
431431
432432
data.weighted(weights).mean()
@@ -444,7 +444,7 @@ If the weights add up to to 0, ``sum`` returns 0:
444444
445445
data.weighted(weights).sum()
446446
447-
and ``mean``, ``std`` and ``var`` return ``NaN``:
447+
and ``mean``, ``std`` and ``var`` return ``nan``:
448448

449449
.. ipython:: python
450450

doc/user-guide/interpolation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ Let's see how :py:meth:`~xarray.DataArray.interp` works on real data.
292292
axes[0].set_title("Raw data")
293293
294294
# Interpolated data
295-
new_lon = np.linspace(ds.lon[0], ds.lon[-1], ds.sizes["lon"] * 4)
296-
new_lat = np.linspace(ds.lat[0], ds.lat[-1], ds.sizes["lat"] * 4)
295+
new_lon = np.linspace(ds.lon[0].item(), ds.lon[-1].item(), ds.sizes["lon"] * 4)
296+
new_lat = np.linspace(ds.lat[0].item(), ds.lat[-1].item(), ds.sizes["lat"] * 4)
297297
dsi = ds.interp(lat=new_lat, lon=new_lon)
298298
dsi.air.plot(ax=axes[1])
299299
@savefig interpolation_sample3.png width=8in

doc/user-guide/testing.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ If the array type you want to generate has an array API-compliant top-level name
239239
you can use this neat trick:
240240

241241
.. ipython:: python
242-
:okwarning:
243242
244-
from numpy import array_api as xp # available in numpy 1.26.0
243+
import numpy as xp # compatible in numpy 2.0
244+
245+
# use `import numpy.array_api as xp` in numpy>=1.23,<2.0
245246
246247
from hypothesis.extra.array_api import make_strategies_namespace
247248

xarray/core/computation.py

+1-46
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ def apply_ufunc(
10641064
supported:
10651065
10661066
>>> magnitude(3, 4)
1067-
5.0
1067+
np.float64(5.0)
10681068
>>> magnitude(3, np.array([0, 4]))
10691069
array([3., 5.])
10701070
>>> magnitude(array, 0)
@@ -1587,15 +1587,6 @@ def cross(
15871587
array([-3, 6, -3])
15881588
Dimensions without coordinates: dim_0
15891589
1590-
Vector cross-product with 2 dimensions, returns in the perpendicular
1591-
direction:
1592-
1593-
>>> a = xr.DataArray([1, 2])
1594-
>>> b = xr.DataArray([4, 5])
1595-
>>> xr.cross(a, b, dim="dim_0")
1596-
<xarray.DataArray ()> Size: 8B
1597-
array(-3)
1598-
15991590
Vector cross-product with 3 dimensions but zeros at the last axis
16001591
yields the same results as with 2 dimensions:
16011592
@@ -1606,42 +1597,6 @@ def cross(
16061597
array([ 0, 0, -3])
16071598
Dimensions without coordinates: dim_0
16081599
1609-
One vector with dimension 2:
1610-
1611-
>>> a = xr.DataArray(
1612-
... [1, 2],
1613-
... dims=["cartesian"],
1614-
... coords=dict(cartesian=(["cartesian"], ["x", "y"])),
1615-
... )
1616-
>>> b = xr.DataArray(
1617-
... [4, 5, 6],
1618-
... dims=["cartesian"],
1619-
... coords=dict(cartesian=(["cartesian"], ["x", "y", "z"])),
1620-
... )
1621-
>>> xr.cross(a, b, dim="cartesian")
1622-
<xarray.DataArray (cartesian: 3)> Size: 24B
1623-
array([12, -6, -3])
1624-
Coordinates:
1625-
* cartesian (cartesian) <U1 12B 'x' 'y' 'z'
1626-
1627-
One vector with dimension 2 but coords in other positions:
1628-
1629-
>>> a = xr.DataArray(
1630-
... [1, 2],
1631-
... dims=["cartesian"],
1632-
... coords=dict(cartesian=(["cartesian"], ["x", "z"])),
1633-
... )
1634-
>>> b = xr.DataArray(
1635-
... [4, 5, 6],
1636-
... dims=["cartesian"],
1637-
... coords=dict(cartesian=(["cartesian"], ["x", "y", "z"])),
1638-
... )
1639-
>>> xr.cross(a, b, dim="cartesian")
1640-
<xarray.DataArray (cartesian: 3)> Size: 24B
1641-
array([-10, 2, 5])
1642-
Coordinates:
1643-
* cartesian (cartesian) <U1 12B 'x' 'y' 'z'
1644-
16451600
Multiple vector cross-products. Note that the direction of the
16461601
cross product vector is defined by the right-hand rule:
16471602

xarray/plot/facetgrid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def _get_largest_lims(self) -> dict[str, tuple[float, float]]:
774774
>>> ds = xr.tutorial.scatter_example_dataset(seed=42)
775775
>>> fg = ds.plot.scatter(x="A", y="B", hue="y", row="x", col="w")
776776
>>> round(fg._get_largest_lims()["x"][0], 3)
777-
-0.334
777+
np.float64(-0.334)
778778
"""
779779
lims_largest: dict[str, tuple[float, float]] = dict(
780780
x=(np.inf, -np.inf), y=(np.inf, -np.inf), z=(np.inf, -np.inf)
@@ -817,7 +817,7 @@ def _set_lims(
817817
>>> fg = ds.plot.scatter(x="A", y="B", hue="y", row="x", col="w")
818818
>>> fg._set_lims(x=(-0.3, 0.3), y=(0, 2), z=(0, 4))
819819
>>> fg.axs[0, 0].get_xlim(), fg.axs[0, 0].get_ylim()
820-
((-0.3, 0.3), (0.0, 2.0))
820+
((np.float64(-0.3), np.float64(0.3)), (np.float64(0.0), np.float64(2.0)))
821821
"""
822822
lims_largest = self._get_largest_lims()
823823

xarray/plot/utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,11 @@ def _update_axes(
811811
def _is_monotonic(coord, axis=0):
812812
"""
813813
>>> _is_monotonic(np.array([0, 1, 2]))
814-
True
814+
np.True_
815815
>>> _is_monotonic(np.array([2, 1, 0]))
816-
True
816+
np.True_
817817
>>> _is_monotonic(np.array([0, 2, 1]))
818-
False
818+
np.False_
819819
"""
820820
if coord.shape[axis] < 3:
821821
return True

0 commit comments

Comments
 (0)