Skip to content

Commit 39136ff

Browse files
authored
Merge pull request #810 from davidhassell/timeduration-mod
Fix failure when creating 'T: mean within days time: minimum over days'' collapse
2 parents 4088c60 + 98241d2 commit 39136ff

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ version NEXTVERSION
99
(https://github.com/NCAS-CMS/cf-python/issues/784)
1010
* Include the UM version as a field property when reading UM files
1111
(https://github.com/NCAS-CMS/cf-python/issues/777)
12+
* Fix bug that caused climatological time collapses within/over days
13+
to fail (https://github.com/NCAS-CMS/cf-python/issues/809)
1214
* New keyword parameter to `cf.Field.derivative`:
1315
``ignore_coordinate_units``
1416
(https://github.com/NCAS-CMS/cf-python/issues/807)

cf/field.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8392,7 +8392,6 @@ def _group_weights(weights, iaxis, index):
83928392
within_days.Units.istime
83938393
and TimeDuration(24, "hours") % within_days
83948394
):
8395-
# % Data(1, 'day'): # % within_days:
83968395
raise ValueError(
83978396
f"Can't collapse: within_days={within_days!r} "
83988397
"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: 5 additions & 7 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,15 +736,14 @@ 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__)):
740+
# 'other' is a number or another TimeDuration object => we
741+
# can use the usual binary operation method.
745742
return self._binary_operation(other, op, aug_assignment)
746743

747744
if may_be_datetime and hasattr(other, "timetuple"):
748-
# other is a date-time object
745+
# 'other' is a date-time object => we must use the special
746+
# datetime arithmetic operation.
749747
try:
750748
return self._datetime_arithmetic(other, getattr(operator, op))
751749
except TypeError:

0 commit comments

Comments
 (0)