Skip to content

Commit 3fc1810

Browse files
committed
within_days collapse
1 parent 5645730 commit 3fc1810

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

Changelog.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ version NEXTVERSION
66
* New method: `cf.Field.is_discrete_axis`
77
(https://github.com/NCAS-CMS/cf-python/issues/784)
88
* Include the UM version as a field property when reading UM files
9-
(https://github.com/NCAS-CMS/cf-python/issues/777)
9+
(https://github.com/NCAS-CMS/cf-python/issues/809)
10+
* Fix bug that caused climatological time collapses within/over days
11+
to fail (https://github.com/NCAS-CMS/cf-python/issues/725)
1012
* Fix bug where `cf.example_fields` returned a `list` of Fields rather
1113
than a `Fieldlist`
1214
(https://github.com/NCAS-CMS/cf-python/issues/725)

cf/field.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8380,7 +8380,6 @@ def _group_weights(weights, iaxis, index):
83808380
within_days.Units.istime
83818381
and TimeDuration(24, "hours") % within_days
83828382
):
8383-
# % Data(1, 'day'): # % within_days:
83848383
raise ValueError(
83858384
f"Can't collapse: within_days={within_days!r} "
83868385
"is not an exact factor of 1 day"

cf/test/test_TimeDuration.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ def test_TimeDuration(self):
107107
self.assertEqual(
108108
cf.TimeDuration(36, "calendar_months") // 8.25, cf.M(4.0)
109109
)
110-
self.assertEqual(cf.TimeDuration(36, "calendar_months") % 10, cf.M(6))
110+
111+
for y in (
112+
10,
113+
cf.Data(10, "calendar_months"),
114+
cf.TimeDuration(10, "calendar_months"),
115+
):
116+
self.assertEqual(
117+
cf.TimeDuration(36, "calendar_months") % y, cf.M(6)
118+
)
119+
120+
for y in (10, cf.Data(10, "hours"), cf.h(10), cf.D(5 / 12), cf.m(600)):
121+
self.assertEqual(cf.h(36) % y, cf.h(6))
111122

112123
self.assertEqual(
113124
cf.TimeDuration(24, "hours") + cf.TimeDuration(0.5, "days"),

cf/test/test_collapse.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ def test_Field_collapse_CLIMATOLOGICAL_TIME(self):
246246
print(g.constructs)
247247
self.assertEqual(list(g.shape), expected_shape)
248248

249+
def test_Field_collapse_CLIMATOLOGICAL_TIME_within_days(self):
250+
verbose = False
251+
252+
f = cf.example_field(5)
253+
254+
g = f.collapse(
255+
"T: mean within days time: minimum over days", within_days=cf.h(12)
256+
)
257+
expected_shape = list(f.shape)
258+
expected_shape[0] = 2
259+
260+
if verbose:
261+
print("\n", f)
262+
print(g)
263+
print(g.constructs)
264+
self.assertEqual(list(g.shape), expected_shape)
265+
249266
def test_Field_collapse(self):
250267
verbose = False
251268

cf/timeduration.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,6 @@ def _apply_binary_comparison(self, other, op):
696696
op: `str`, the binary comparison operator to apply.
697697
698698
"""
699-
700699
if isinstance(other, (self.__class__, int, float)):
701700
return bool(self._binary_operation(other, op))
702701

@@ -737,11 +736,7 @@ def _apply_binary_arithmetic(
737736
do not skip.
738737
739738
"""
740-
check_simple_types = [int, float]
741-
if may_be_datetime:
742-
check_simple_types.append(self.__class__)
743-
744-
if isinstance(other, tuple(check_simple_types)):
739+
if isinstance(other, (int, float, self.__class__)):
745740
return self._binary_operation(other, op, aug_assignment)
746741

747742
if may_be_datetime and hasattr(other, "timetuple"):

0 commit comments

Comments
 (0)