Commit 98cfee5
Allow
* Allow `mean` with time dtypes
Closes #5897
Closes #6995
Closes #10217
* Allow mean() to work with datetime dtypes
- Changed numeric_only from True to False in mean() aggregations
- Added Dataset.mean() override to filter out string variables while preserving datetime/timedelta types
- Only filters data variables, preserves all coordinates
- Added comprehensive tests for datetime mean with edge cases including NaT handling
- Tests cover Dataset, DataArray, groupby, and mixed type scenarios
This enables mean() to work with datetime64 and timedelta64 types as requested in PR #10227 while preventing errors from string variables.
* Skip cftime test when cftime not available
Add pytest.mark.skipif decorator to test_mean_preserves_non_string_object_arrays
to skip the test in minimal dependency environments where cftime is not installed.
* Fix string/datetime handling in mean() aggregations
- Revert numeric_only back to True for mean() to prevent strings from being included
- Add datetime64/timedelta64 types to numeric_only checks in Dataset.reduce() and flox path
- Also check for object arrays containing datetime-like objects (cftime dates)
- This allows mean() to work with datetime types while excluding strings that would cause errors
* Trigger CI workflow
* Format groupby.py numeric_only condition
Auto-formatted the multi-line condition for better readability
* Apply formatting changes to dataset.py and test_groupby.py
- Auto-formatted multi-line conditions for better readability
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix numeric_only parameter in groupby mean for non-flox path
The generated mean() methods for DatasetGroupBy and ResampleGroupBy
were incorrectly passing numeric_only=False in the non-flox path,
causing string variables to fail during reduction. Changed to
numeric_only=True to match the flox path behavior.
This fixes test_groupby_dataset_reduce_ellipsis failures.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Simplify dtype checking with _is_numeric_aggregatable_dtype helper
Created a canonical helper function to check if a dtype can be used in
numeric aggregations. This replaces complex repeated conditionals in
dataset.py and groupby.py with a single, well-documented function.
The helper checks for:
- Numeric types (int, float, complex)
- Boolean type
- Datetime types (datetime64, timedelta64)
- Object arrays containing datetime-like objects (e.g., cftime)
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Address review feedback from @dcherian
- Document datetime mean behavior in generate_aggregations.py
- Simplify test assertion using isnull().item() instead of pd.notna
- Remove redundant test_mean_dataarray_datetime test
- Add comprehensive tests for linked issues:
- Issue #5897: Test mean with cftime objects
- Issue #6995: Test groupby_bins with datetime64 mean
- Issue #10217: Test groupby_bins mean on time series data
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Fix test_mean_with_cftime_objects for non-dask environments
The test was unconditionally using dask operations which caused failures
in the "all-but-dask" CI environment. Now the dask-specific tests are
only run when dask is available.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Use standard @requires_dask decorator for dask-specific tests
Split test_mean_with_cftime_objects into two tests:
- Base test runs without dask
- Dask-specific test uses @requires_dask decorator
This follows xarray's standard pattern for dependency-specific tests
and is cleaner than using if has_dask conditionals.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Add testing guidelines and use proper decorators
- Created xarray/tests/CLAUDE.md with comprehensive guidelines for
handling optional dependencies in tests
- Updated cftime tests to use @requires_cftime decorator instead of
pytest.importorskip, following xarray's standard patterns
- This ensures consistent handling across CI environments
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CLAUDE.md blackdoc formatting
Ensure all Python code blocks have complete, valid syntax for blackdoc.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Simplify cftime check in _is_numeric_aggregatable_dtype
As suggested by @dcherian in review comment r2337966239, directly use
_contains_cftime_datetimes(var._data) instead of the more complex check.
This is cleaner since _contains_cftime_datetimes already handles the
object dtype check internally.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Streamline CLAUDE.md and move import to top of file
- Made CLAUDE.md more concise by removing verbose decorator listings
- Moved _is_numeric_aggregatable_dtype import to top of groupby.py
as suggested by @dcherian in review comment r2337968851
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Address review comments: clarify numeric_only scope and merge tests
- Move datetime/timedelta note to global _NUMERIC_ONLY_NOTES constant
(addresses comment r2337970143)
- Merge test_mean_preserves_non_string_object_arrays into
test_mean_with_cftime_objects (addresses comment r2337128692)
- Both changes address reviewer feedback about simplification
* Clarify that @requires decorators should be used instead of skipif patterns
Update testing guidelines to explicitly discourage pytest.mark.skipif
in parametrize, recommending splitting tests or using @requires decorators
* Fix CLAUDE.md blackdoc formatting
Co-authored-by: Claude <no-reply@anthropic.com>
---------
Co-authored-by: Maximilian Roos <m@maxroos.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Claude <no-reply@anthropic.com>mean with time dtypes (#10227)1 parent dff84af commit 98cfee5
File tree
8 files changed
+387
-25
lines changed- xarray
- core
- namedarray
- tests
- util
8 files changed
+387
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1776 | 1776 | | |
1777 | 1777 | | |
1778 | 1778 | | |
1779 | | - | |
1780 | | - | |
1781 | | - | |
1782 | | - | |
1783 | 1779 | | |
1784 | 1780 | | |
1785 | 1781 | | |
| |||
2948 | 2944 | | |
2949 | 2945 | | |
2950 | 2946 | | |
2951 | | - | |
2952 | | - | |
2953 | | - | |
2954 | | - | |
2955 | 2947 | | |
2956 | 2948 | | |
2957 | 2949 | | |
| |||
4231 | 4223 | | |
4232 | 4224 | | |
4233 | 4225 | | |
4234 | | - | |
4235 | | - | |
4236 | 4226 | | |
4237 | 4227 | | |
4238 | 4228 | | |
| |||
5729 | 5719 | | |
5730 | 5720 | | |
5731 | 5721 | | |
5732 | | - | |
5733 | | - | |
5734 | 5722 | | |
5735 | 5723 | | |
5736 | 5724 | | |
| |||
7188 | 7176 | | |
7189 | 7177 | | |
7190 | 7178 | | |
7191 | | - | |
7192 | | - | |
7193 | 7179 | | |
7194 | 7180 | | |
7195 | 7181 | | |
| |||
8578 | 8564 | | |
8579 | 8565 | | |
8580 | 8566 | | |
8581 | | - | |
8582 | | - | |
8583 | 8567 | | |
8584 | 8568 | | |
8585 | 8569 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2108 | 2108 | | |
2109 | 2109 | | |
2110 | 2110 | | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
6847 | 6848 | | |
6848 | 6849 | | |
6849 | 6850 | | |
6850 | | - | |
6851 | | - | |
| 6851 | + | |
6852 | 6852 | | |
6853 | 6853 | | |
6854 | 6854 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| |||
1068 | 1072 | | |
1069 | 1073 | | |
1070 | 1074 | | |
1071 | | - | |
| 1075 | + | |
1072 | 1076 | | |
1073 | 1077 | | |
1074 | 1078 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | 355 | | |
360 | 356 | | |
361 | 357 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
0 commit comments