@@ -2711,6 +2711,80 @@ def interp(
2711
2711
--------
2712
2712
scipy.interpolate.interp1d
2713
2713
scipy.interpolate.interpn
2714
+
2715
+ Examples
2716
+ --------
2717
+ >>> ds = xr.Dataset(
2718
+ ... data_vars={
2719
+ ... "a": ("x", [5, 7, 4]),
2720
+ ... "b": (
2721
+ ... ("x", "y"),
2722
+ ... [[1, 4, 2, 9], [2, 7, 6, np.nan], [6, np.nan, 5, 8]],
2723
+ ... ),
2724
+ ... },
2725
+ ... coords={"x": [0, 1, 2], "y": [10, 12, 14, 16]},
2726
+ ... )
2727
+ >>> ds
2728
+ <xarray.Dataset>
2729
+ Dimensions: (x: 3, y: 4)
2730
+ Coordinates:
2731
+ * x (x) int64 0 1 2
2732
+ * y (y) int64 10 12 14 16
2733
+ Data variables:
2734
+ a (x) int64 5 7 4
2735
+ b (x, y) float64 1.0 4.0 2.0 9.0 2.0 7.0 6.0 nan 6.0 nan 5.0 8.0
2736
+
2737
+ 1D interpolation with the default method (linear):
2738
+
2739
+ >>> ds.interp(x=[0, 0.75, 1.25, 1.75])
2740
+ <xarray.Dataset>
2741
+ Dimensions: (x: 4, y: 4)
2742
+ Coordinates:
2743
+ * y (y) int64 10 12 14 16
2744
+ * x (x) float64 0.0 0.75 1.25 1.75
2745
+ Data variables:
2746
+ a (x) float64 5.0 6.5 6.25 4.75
2747
+ b (x, y) float64 1.0 4.0 2.0 nan 1.75 6.25 ... nan 5.0 nan 5.25 nan
2748
+
2749
+ 1D interpolation with a different method:
2750
+
2751
+ >>> ds.interp(x=[0, 0.75, 1.25, 1.75], method="nearest")
2752
+ <xarray.Dataset>
2753
+ Dimensions: (x: 4, y: 4)
2754
+ Coordinates:
2755
+ * y (y) int64 10 12 14 16
2756
+ * x (x) float64 0.0 0.75 1.25 1.75
2757
+ Data variables:
2758
+ a (x) float64 5.0 7.0 7.0 4.0
2759
+ b (x, y) float64 1.0 4.0 2.0 9.0 2.0 7.0 ... 6.0 nan 6.0 nan 5.0 8.0
2760
+
2761
+ 1D extrapolation:
2762
+
2763
+ >>> ds.interp(
2764
+ ... x=[1, 1.5, 2.5, 3.5],
2765
+ ... method="linear",
2766
+ ... kwargs={"fill_value": "extrapolate"},
2767
+ ... )
2768
+ <xarray.Dataset>
2769
+ Dimensions: (x: 4, y: 4)
2770
+ Coordinates:
2771
+ * y (y) int64 10 12 14 16
2772
+ * x (x) float64 1.0 1.5 2.5 3.5
2773
+ Data variables:
2774
+ a (x) float64 7.0 5.5 2.5 -0.5
2775
+ b (x, y) float64 2.0 7.0 6.0 nan 4.0 nan ... 4.5 nan 12.0 nan 3.5 nan
2776
+
2777
+ 2D interpolation:
2778
+
2779
+ >>> ds.interp(x=[0, 0.75, 1.25, 1.75], y=[11, 13, 15], method="linear")
2780
+ <xarray.Dataset>
2781
+ Dimensions: (x: 4, y: 3)
2782
+ Coordinates:
2783
+ * x (x) float64 0.0 0.75 1.25 1.75
2784
+ * y (y) int64 11 13 15
2785
+ Data variables:
2786
+ a (x) float64 5.0 6.5 6.25 4.75
2787
+ b (x, y) float64 2.5 3.0 nan 4.0 5.625 nan nan nan nan nan nan nan
2714
2788
"""
2715
2789
from . import missing
2716
2790
@@ -4244,6 +4318,50 @@ def interpolate_na(
4244
4318
--------
4245
4319
numpy.interp
4246
4320
scipy.interpolate
4321
+
4322
+ Examples
4323
+ --------
4324
+ >>> ds = xr.Dataset(
4325
+ ... {
4326
+ ... "A": ("x", [np.nan, 2, 3, np.nan, 0]),
4327
+ ... "B": ("x", [3, 4, np.nan, 1, 7]),
4328
+ ... "C": ("x", [np.nan, np.nan, np.nan, 5, 0]),
4329
+ ... "D": ("x", [np.nan, 3, np.nan, -1, 4]),
4330
+ ... },
4331
+ ... coords={"x": [0, 1, 2, 3, 4]},
4332
+ ... )
4333
+ >>> ds
4334
+ <xarray.Dataset>
4335
+ Dimensions: (x: 5)
4336
+ Coordinates:
4337
+ * x (x) int64 0 1 2 3 4
4338
+ Data variables:
4339
+ A (x) float64 nan 2.0 3.0 nan 0.0
4340
+ B (x) float64 3.0 4.0 nan 1.0 7.0
4341
+ C (x) float64 nan nan nan 5.0 0.0
4342
+ D (x) float64 nan 3.0 nan -1.0 4.0
4343
+
4344
+ >>> ds.interpolate_na(dim="x", method="linear")
4345
+ <xarray.Dataset>
4346
+ Dimensions: (x: 5)
4347
+ Coordinates:
4348
+ * x (x) int64 0 1 2 3 4
4349
+ Data variables:
4350
+ A (x) float64 nan 2.0 3.0 1.5 0.0
4351
+ B (x) float64 3.0 4.0 2.5 1.0 7.0
4352
+ C (x) float64 nan nan nan 5.0 0.0
4353
+ D (x) float64 nan 3.0 1.0 -1.0 4.0
4354
+
4355
+ >>> ds.interpolate_na(dim="x", method="linear", fill_value="extrapolate")
4356
+ <xarray.Dataset>
4357
+ Dimensions: (x: 5)
4358
+ Coordinates:
4359
+ * x (x) int64 0 1 2 3 4
4360
+ Data variables:
4361
+ A (x) float64 1.0 2.0 3.0 1.5 0.0
4362
+ B (x) float64 3.0 4.0 2.5 1.0 7.0
4363
+ C (x) float64 20.0 15.0 10.0 5.0 0.0
4364
+ D (x) float64 5.0 3.0 1.0 -1.0 4.0
4247
4365
"""
4248
4366
from .missing import _apply_over_vars_with_dim , interp_na
4249
4367
@@ -5794,6 +5912,7 @@ def filter_by_attrs(self, **kwargs):
5794
5912
Examples
5795
5913
--------
5796
5914
>>> # Create an example dataset:
5915
+ ...
5797
5916
>>> import numpy as np
5798
5917
>>> import pandas as pd
5799
5918
>>> import xarray as xr
@@ -5974,6 +6093,7 @@ def map_blocks(
5974
6093
... gb = da.groupby(groupby_type)
5975
6094
... clim = gb.mean(dim="time")
5976
6095
... return gb - clim
6096
+ ...
5977
6097
>>> time = xr.cftime_range("1990-01", "1992-01", freq="M")
5978
6098
>>> month = xr.DataArray(time.month, coords={"time": time}, dims=["time"])
5979
6099
>>> np.random.seed(123)
0 commit comments