Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 23, 2024
1 parent 97b00a4 commit 1626489
Showing 1 changed file with 54 additions and 47 deletions.
101 changes: 54 additions & 47 deletions xarray/tests/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_interpolate_pd_compat(method, fill_value, dim, shape, frac_nan) -> None
method=method,
axis=da.get_axis_num(dim),
fill_value=fill_value,
limit_direction='both'
limit_direction="both",
)

if method == "linear":
Expand Down Expand Up @@ -344,12 +344,12 @@ def test_interp1d_fastrack(method, vals):
@requires_bottleneck
def test_interpolate_limits():
n = np.nan
times=pd.date_range("2000-01-01", periods=9, freq="2h")
times = pd.date_range("2000-01-01", periods=9, freq="2h")
coords = {"yt": ("y", times)}
da = xr.DataArray([n,n,3, n, n, 6, n, 8, n], dims=["y"], coords=coords)
da = xr.DataArray([n, n, 3, n, n, 6, n, 8, n], dims=["y"], coords=coords)

actual = da.interpolate_na(dim="y", limit=None, fill_value="extrapolate")
#With no limit, everything should be interpolated. Introduced in xarray due to a bug (GH7665), but kept for backward compatibility
# With no limit, everything should be interpolated. Introduced in xarray due to a bug (GH7665), but kept for backward compatibility
expected = da.copy(data=[1, 2, 3, 4, 5, 6, 7, 8, 9])
assert_equal(actual, expected)

Expand All @@ -365,6 +365,7 @@ def test_interpolate_limits():
expected = da.copy(data=[n, n, 3, n, n, 6, 7, 8, 9])
assert_equal(actual, expected)


def test_interpolate_double_coordinate():
# Check if max_gap is able to handle string coordinate names
# Limit is always refering to an index
Expand All @@ -375,11 +376,8 @@ def test_interpolate_double_coordinate():
coords={"y1": ("y", np.arange(7)), "y2": ("y", np.arange(7) * 2)},
)
actual = da.interpolate_na(

Check failure on line 378 in xarray/tests/test_missing.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_interpolate_double_coordinate ModuleNotFoundError: No module named 'bottleneck'
dim="y",
limit=1,
max_gap=4,
use_coordinate="y1",
fill_value="extrapolate")
dim="y", limit=1, max_gap=4, use_coordinate="y1", fill_value="extrapolate"
)
expected = da.copy(data=[[1, 2, n, 4, 5, 6, 7], [1, 2, n, n, 5, 6, n]])
assert_equal(actual, expected)

Expand Down Expand Up @@ -1024,7 +1022,9 @@ def test_interpolate_na_max_gap_2d(coords):
coords=coords,
)

actual = da.interpolate_na("y", use_coordinate=use_coordinate, max_gap=2, fill_value='extrapolate')
actual = da.interpolate_na(
"y", use_coordinate=use_coordinate, max_gap=2, fill_value="extrapolate"
)
expected_y = da.copy(
data=[
[1, 2, 3, 4, 5, 6, n, n, n, 10, 11, 12],
Expand All @@ -1048,7 +1048,9 @@ def test_interpolate_na_max_gap_2d(coords):
)
assert_equal(actual, expected_y_extra)

actual = da.interpolate_na("x", use_coordinate=use_coordinate, max_gap=3, fill_value="extrapolate")
actual = da.interpolate_na(
"x", use_coordinate=use_coordinate, max_gap=3, fill_value="extrapolate"
)
expected_x = xr.DataArray(
[
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, n],
Expand All @@ -1061,9 +1063,10 @@ def test_interpolate_na_max_gap_2d(coords):
)
assert_equal(actual, expected_x)


def test_interpolate_na_limit_2d():
n = np.nan
times=pd.date_range("2000-01-01", periods=12, freq="3h")
times = pd.date_range("2000-01-01", periods=12, freq="3h")
coords = {
"x": np.arange(3) * 2,
"time": (times),
Expand All @@ -1087,6 +1090,7 @@ def test_interpolate_na_limit_2d():
)
assert_equal(actual, expected)


@requires_scipy
def test_interpolators_complex_out_of_bounds():
"""Ensure complex nans are used for complex data"""
Expand All @@ -1107,26 +1111,31 @@ def test_interpolators_complex_out_of_bounds():
actual = f(x)
assert_array_equal(actual, expected)


####Masking Functionality
def test_fill_gaps_limit():
n = np.nan
times=pd.date_range("2000-01-01", periods=8, freq="2h")
times = pd.date_range("2000-01-01", periods=8, freq="2h")
coords = {"yt": ("y", times)}
da = xr.DataArray([n, n, 2, n, n, 5, n, n], dims=["y"], coords=coords)

actual = da.fill_gaps(dim='y', limit=None).interpolate_na(dim="y", fill_value="extrapolate")
actual = da.fill_gaps(dim="y", limit=None).interpolate_na(

Check failure on line 1122 in xarray/tests/test_missing.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_fill_gaps_limit ModuleNotFoundError: No module named 'scipy'
dim="y", fill_value="extrapolate"
)
expected = da.copy(data=[0, 1, 2, 3, 4, 5, 6, 7])
assert_equal(actual, expected)

actual = da.fill_gaps(dim='y', limit=1).interpolate_na(dim="y", fill_value="extrapolate")
actual = da.fill_gaps(dim="y", limit=1).interpolate_na(
dim="y", fill_value="extrapolate"
)
expected = da.copy(data=[n, 1, 2, 3, 4, 5, 6, n])
assert_equal(actual, expected)

actual = da.fill_gaps(
dim="y",
limit=pd.Timedelta("3h"),
use_coordinate="yt",
).interpolate_na(dim='y', fill_value="extrapolate")
).interpolate_na(dim="y", fill_value="extrapolate")
expected = da.copy(data=[n, 1, 2, 3, 4, 5, 6, n])
assert_equal(actual, expected)

Expand All @@ -1135,13 +1144,14 @@ def test_fill_gaps_limit():
limit=pd.Timedelta("3h"),
limit_direction="backward",
use_coordinate="yt",
).interpolate_na(dim='y', fill_value="extrapolate")
).interpolate_na(dim="y", fill_value="extrapolate")
expected = da.copy(data=[n, 1, 2, n, 4, 5, n, n])
assert_equal(actual, expected)


def test_mask_gap_limit_2d():
n = np.nan
times=pd.date_range("2000-01-01", periods=12, freq="3h")
times = pd.date_range("2000-01-01", periods=12, freq="3h")
coords = {
"x": np.arange(3) * 2,
"time": (times),
Expand All @@ -1155,8 +1165,8 @@ def test_mask_gap_limit_2d():
coords=coords,
)

mask = da.fill_gaps('time', limit=1, use_coordinate=False)
actual=mask.interpolate_na("time", fill_value="extrapolate")
mask = da.fill_gaps("time", limit=1, use_coordinate=False)
actual = mask.interpolate_na("time", fill_value="extrapolate")
expected = da.copy(
data=[
[1, 2, 3, 4, 5, 6, 7, n, 9, 10, 11, 12],
Expand All @@ -1165,7 +1175,7 @@ def test_mask_gap_limit_2d():
]
)
assert_equal(actual, expected)
actual=mask.ffill(dim="time")
actual = mask.ffill(dim="time")
expected = da.copy(
data=[
[1, 2, 3, 4, 4, 6, 6, n, 6, 10, 11, 11],
Expand All @@ -1174,7 +1184,7 @@ def test_mask_gap_limit_2d():
]
)
assert_equal(actual, expected)
actual=mask.fillna(0)
actual = mask.fillna(0)
expected = da.copy(
data=[
[1, 2, 3, 4, 0, 6, 0, n, 0, 10, 11, 0],
Expand All @@ -1184,9 +1194,9 @@ def test_mask_gap_limit_2d():
)
assert_equal(actual, expected)

actual = da.fill_gaps('time', limit=2, use_coordinate=False, limit_direction='backward').interpolate_na(
"time", fill_value="extrapolate"
)
actual = da.fill_gaps(
"time", limit=2, use_coordinate=False, limit_direction="backward"
).interpolate_na("time", fill_value="extrapolate")
expected = da.copy(
data=[
[1, 2, 3, 4, 5, 6, n, 8, 9, 10, 11, n],
Expand All @@ -1201,9 +1211,9 @@ def test_mask_gap_limit_2d():
limit=pd.Timedelta("3h"),
limit_direction="backward",
limit_area="inside",
use_coordinate=True
use_coordinate=True,
).interpolate_na(
'time',
"time",
fill_value="extrapolate",
)
expected = da.copy(
Expand All @@ -1219,9 +1229,9 @@ def test_mask_gap_limit_2d():
limit=pd.Timedelta("3h"),
limit_direction="backward",
limit_area="outside",
use_coordinate=True
use_coordinate=True,
).interpolate_na(
'time',
"time",
fill_value="extrapolate",
)
expected = da.copy(
Expand All @@ -1238,9 +1248,9 @@ def test_mask_gap_limit_2d():
limit=None,
limit_direction="backward",
limit_area="outside",
use_coordinate=True
use_coordinate=True,
).interpolate_na(
'time',
"time",
fill_value=8,
)
expected = da.copy(
Expand All @@ -1267,9 +1277,9 @@ def test_mask_gap_limit_2d():
limit=3,
limit_direction="forward",
limit_area=None,
use_coordinate=True
use_coordinate=True,
).interpolate_na(
'x',
"x",
fill_value="extrapolate",
method="linear",
)
Expand All @@ -1283,9 +1293,10 @@ def test_mask_gap_limit_2d():
)
assert_equal(actual, expected)


def test_mask_gap_max_gap_2d():
n = np.nan
times=pd.date_range("2000-01-01", periods=12, freq="3h")
times = pd.date_range("2000-01-01", periods=12, freq="3h")
coords = {
"x": np.arange(3) * 2,
"time": (times),
Expand All @@ -1299,8 +1310,8 @@ def test_mask_gap_max_gap_2d():
coords=coords,
)

mask = da.fill_gaps('time', max_gap=1, use_coordinate=False)
actual=mask.interpolate_na("time", fill_value="extrapolate")
mask = da.fill_gaps("time", max_gap=1, use_coordinate=False)
actual = mask.interpolate_na("time", fill_value="extrapolate")
expected = da.copy(
data=[
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, 12],
Expand All @@ -1309,8 +1320,8 @@ def test_mask_gap_max_gap_2d():
]
)
assert_equal(actual, expected)
mask = da.fill_gaps('time', max_gap=2, use_coordinate=False)
actual=mask.interpolate_na("time", fill_value="extrapolate")
mask = da.fill_gaps("time", max_gap=2, use_coordinate=False)
actual = mask.interpolate_na("time", fill_value="extrapolate")
expected = da.copy(
data=[
[1, 2, 3, 4, 5, 6, n, n, n, 10, 11, 12],
Expand All @@ -1320,8 +1331,8 @@ def test_mask_gap_max_gap_2d():
)
assert_equal(actual, expected)

mask = da.fill_gaps('time', max_gap=pd.Timedelta("3h"), use_coordinate=True)
actual=mask.interpolate_na("time", fill_value="extrapolate")
mask = da.fill_gaps("time", max_gap=pd.Timedelta("3h"), use_coordinate=True)
actual = mask.interpolate_na("time", fill_value="extrapolate")
expected = da.copy(
data=[
[1, 2, 3, 4, n, 6, n, n, n, 10, 11, 12],
Expand All @@ -1331,6 +1342,7 @@ def test_mask_gap_max_gap_2d():
)
assert_equal(actual, expected)


def test_mask_double_coordinate():
# Check if limit and max_gap are able to handle string coordinate names
n = np.nan
Expand All @@ -1348,14 +1360,9 @@ def test_mask_double_coordinate():
expected = da.copy(data=[[1, 2, 3, 4, 5, 6, 7], [1, 2, n, 4, 5, 6, n]])
assert_equal(actual, expected)

actual = da.fill_gaps(
"y",
limit=2,
max_gap=4,
use_coordinate="y2"
).interpolate_na(
actual = da.fill_gaps("y", limit=2, max_gap=4, use_coordinate="y2").interpolate_na(
"y",
fill_value="extrapolate",
)
expected = da.copy(data=[[1, n, n, 4, 5, 6, 7], [1, n, n, n, 5, 6, n]])
assert_equal(actual, expected)
assert_equal(actual, expected)

0 comments on commit 1626489

Please sign in to comment.