MCPcopy Index your code
hub / github.com/pydata/xarray / make_interpolate_example_data

Function make_interpolate_example_data

xarray/tests/test_missing.py:71–101  ·  view source on GitHub ↗
(shape, frac_nan, seed=12345, non_uniform=False)

Source from the content-addressed store, hash-verified

69
70
71def make_interpolate_example_data(shape, frac_nan, seed=12345, non_uniform=False):
72 rs = np.random.default_rng(seed)
73 vals = rs.normal(size=shape)
74 if frac_nan == 1:
75 vals[:] = np.nan
76 elif frac_nan == 0:
77 pass
78 else:
79 n_missing = int(vals.size * frac_nan)
80
81 ys = np.arange(shape[0])
82 xs = np.arange(shape[1])
83 if n_missing:
84 np.random.shuffle(ys)
85 ys = ys[:n_missing]
86
87 np.random.shuffle(xs)
88 xs = xs[:n_missing]
89
90 vals[ys, xs] = np.nan
91
92 if non_uniform:
93 # construct a datetime index that has irregular spacing
94 deltas = pd.to_timedelta(rs.normal(size=shape[0], scale=10), unit="D")
95 coords = {"time": (pd.Timestamp("2000-01-01") + deltas).sort_values()}
96 else:
97 coords = {"time": pd.date_range("2000-01-01", freq="D", periods=shape[0])}
98 da = xr.DataArray(vals, dims=("time", "x"), coords=coords)
99 df = da.to_pandas()
100
101 return da, df
102
103
104@pytest.mark.parametrize("fill_value", [None, np.nan, 47.11])

Calls 3

to_pandasMethod · 0.95
arangeMethod · 0.80
shuffleMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…