Skip to content

Commit 98ed32e

Browse files
committed
Add a few tests
1 parent fb5b288 commit 98ed32e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

xarray/test/test_conventions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ def test_decode_cf_with_conflicting_fill_missing_value(self):
228228
self.assertRaisesRegexp(ValueError, "_FillValue and missing_value",
229229
lambda: conventions.decode_cf_variable(var))
230230

231+
def test_decoded_timedelta_array(self):
232+
actual = conventions.DecodedCFTimedeltaArray(
233+
np.array([0, 1, 2]), 'seconds')
234+
expected = pd.to_timedelta(['0s', '1s', '2s']).values
235+
self.assertEqual(actual.dtype, np.dtype('timedelta64[ns]'))
236+
self.assertArrayEqual(actual, expected)
237+
231238
@requires_netCDF4
232239
def test_decode_cf_datetime_non_iso_strings(self):
233240
# datetime strings that are _almost_ ISO compliant but not quite,
@@ -537,6 +544,41 @@ def test_decode_cf_with_drop_variables(self):
537544
self.assertDatasetIdentical(expected, actual)
538545
self.assertDatasetIdentical(expected, actual2)
539546

547+
def test_datetimes_true(self):
548+
original = Dataset({
549+
'time': ('time', [0, 1, 2], {'units': 'days since 2000-01-01'}),
550+
'period': ('time', [0, 1, 2], {'coordinates': 'time', 'units': 'seconds'}),
551+
})
552+
expected = Dataset({
553+
'time': pd.date_range('2000-01-01', periods=3),
554+
'period': ('time', [0, 1, 2], {'units': 'seconds'}),
555+
})
556+
actual = conventions.decode_cf(original, decode_datetimes=True)
557+
self.assertDatasetIdentical(expected, actual)
558+
559+
def test_timedeltas_true(self):
560+
original = Dataset({
561+
'time': ('time', [0, 1, 2], {'units': 'days since 2000-01-01'}),
562+
'period': ('time', [0, 1, 2], {'coordinates': 'time', 'units': 'seconds'}),
563+
})
564+
expected = Dataset({
565+
'time': pd.date_range('2000-01-01', periods=3),
566+
'period': ('time', pd.to_timedelta(['0s', '1s', '2s'])),
567+
})
568+
actual = conventions.decode_cf(original, decode_timedeltas=True)
569+
self.assertDatasetIdentical(expected, actual)
570+
571+
def test_datetimes_false(self):
572+
original = Dataset({
573+
'time': ('time', [0, 1, 2], {'units': 'days since 2000-01-01'}),
574+
'period': ('time', [0, 1, 2], {'coordinates': 'time', 'units': 'seconds'}),
575+
})
576+
expected = Dataset({
577+
'time': [0, 1, 2],
578+
'period': ('time', [0, 1, 2], {'units': 'seconds'}),
579+
})
580+
actual = conventions.decode_cf(original, decode_datetimes=False)
581+
self.assertArrayEqual(expected['time'], actual['time'])
540582

541583
class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore):
542584
pass

0 commit comments

Comments
 (0)