-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Enable taking the mean of dask-backed cftime arrays #6940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9df6729
Enable taking the mean of dask-backed cftime arrays
spencerkclark c3e8bf9
Add requires_dask decorator
spencerkclark 47d5596
Address dask compatibility issue
spencerkclark e2b46f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 09cc087
Uncomment pytest.skip
spencerkclark 5ace485
Merge branch 'dask-cftime-mean' of https://github.com/spencerkclark/x…
spencerkclark 8b1d5cd
Bump minimum dask version; remove compatibility code
spencerkclark 3b7a30c
Merge branch 'main' into dask-cftime-mean
spencerkclark e8a3eb3
Remove unused import
spencerkclark 1c59580
Update min-all-deps.yml
spencerkclark caf1116
Merge branch 'main' into dask-cftime-mean
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,18 +323,51 @@ def test_datetime_mean(dask: bool) -> None: | |
|
||
|
||
@requires_cftime | ||
def test_cftime_datetime_mean(): | ||
@pytest.mark.parametrize("dask", [False, True]) | ||
def test_cftime_datetime_mean(dask): | ||
if dask and not has_dask: | ||
pytest.skip("requires dask") | ||
|
||
times = cftime_range("2000", periods=4) | ||
da = DataArray(times, dims=["time"]) | ||
da_2d = DataArray(times.values.reshape(2, 2)) | ||
|
||
assert da.isel(time=0).mean() == da.isel(time=0) | ||
if dask: | ||
da = da.chunk({"time": 2}) | ||
da_2d = da_2d.chunk({"dim_0": 2}) | ||
|
||
expected = da.isel(time=0) | ||
# one compute needed to check the array contains cftime datetimes | ||
with raise_if_dask_computes(max_computes=1): | ||
result = da.isel(time=0).mean() | ||
assert_dask_array(result, dask) | ||
assert_equal(result, expected) | ||
|
||
expected = DataArray(times.date_type(2000, 1, 2, 12)) | ||
result = da.mean() | ||
with raise_if_dask_computes(max_computes=1): | ||
result = da.mean() | ||
assert_dask_array(result, dask) | ||
assert_equal(result, expected) | ||
|
||
da_2d = DataArray(times.values.reshape(2, 2)) | ||
result = da_2d.mean() | ||
with raise_if_dask_computes(max_computes=1): | ||
result = da_2d.mean() | ||
assert_dask_array(result, dask) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! Thanks for making extra sure that we didn't compute the whole thing. |
||
assert_equal(result, expected) | ||
|
||
|
||
@requires_cftime | ||
@requires_dask | ||
def test_mean_over_non_time_dim_of_dataset_with_dask_backed_cftime_data(): | ||
# Regression test for part two of GH issue 5897: averaging over a non-time | ||
# dimension still fails if the time variable is dask-backed. | ||
ds = Dataset( | ||
{ | ||
"var1": (("time",), cftime_range("2021-10-31", periods=10, freq="D")), | ||
"var2": (("x",), list(range(10))), | ||
} | ||
) | ||
expected = ds.mean("x") | ||
result = ds.chunk({}).mean("x") | ||
assert_equal(result, expected) | ||
|
||
|
||
|
@@ -372,15 +405,6 @@ def test_cftime_datetime_mean_long_time_period(): | |
assert_equal(result, expected) | ||
|
||
|
||
@requires_cftime | ||
@requires_dask | ||
def test_cftime_datetime_mean_dask_error(): | ||
times = cftime_range("2000", periods=4) | ||
da = DataArray(times, dims=["time"]).chunk() | ||
with pytest.raises(NotImplementedError): | ||
da.mean() | ||
|
||
|
||
def test_empty_axis_dtype(): | ||
ds = Dataset() | ||
ds["pos"] = [1, 2, 3] | ||
|
@@ -742,6 +766,17 @@ def test_datetime_to_numeric_cftime(dask): | |
expected = 24 * np.arange(0, 35, 7).astype(dtype) | ||
np.testing.assert_array_equal(result, expected) | ||
|
||
with raise_if_dask_computes(): | ||
if dask: | ||
time = dask.array.asarray(times[1]) | ||
else: | ||
time = np.asarray(times[1]) | ||
result = duck_array_ops.datetime_to_numeric( | ||
time, offset=times[0], datetime_unit="h", dtype=int | ||
) | ||
expected = np.array(24 * 7).astype(int) | ||
np.testing.assert_array_equal(result, expected) | ||
|
||
|
||
@requires_cftime | ||
def test_datetime_to_numeric_potential_overflow(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.