From b2ade2e4d2eda90dd09fde8ff717ee3dd1749332 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 25 Oct 2018 17:30:22 -0700 Subject: [PATCH 1/3] Make sure datetime object arrays are converted to datetime64 Fixes #2512 --- xarray/core/variable.py | 2 +- xarray/tests/test_variable.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index c003d52aab2..fefd48b449c 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -95,7 +95,7 @@ def as_variable(obj, name=None): 'cannot set variable %r with %r-dimensional data ' 'without explicit dimension names. Pass a tuple of ' '(dims, data) instead.' % (name, data.ndim)) - obj = Variable(name, obj, fastpath=True) + obj = Variable(name, data, fastpath=True) else: raise TypeError('unable to convert object into a variable without an ' 'explicit list of dimensions: %r' % obj) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 52289a15d72..46432008f48 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -992,6 +992,11 @@ def test_as_variable(self): ValueError, 'has more than 1-dimension'): as_variable(expected, name='x') + # test datetime conversion + dt = datetime(1999, 1, 1) + dts = np.array([dt + timedelta(days=x) for x in range(10)]) + assert as_variable(dts, 'time').dtype.kind == 'M' + def test_repr(self): v = Variable(['time', 'x'], [[1, 2, 3], [4, 5, 6]], {'foo': 'bar'}) expected = dedent(""" From dd036914c72f3e25c0346b6672f28167e1de2597 Mon Sep 17 00:00:00 2001 From: dcherian Date: Fri, 26 Oct 2018 05:16:19 -0700 Subject: [PATCH 2/3] Add timedelta conversion test. --- xarray/tests/test_variable.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 46432008f48..3753221f352 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -992,10 +992,12 @@ def test_as_variable(self): ValueError, 'has more than 1-dimension'): as_variable(expected, name='x') - # test datetime conversion - dt = datetime(1999, 1, 1) - dts = np.array([dt + timedelta(days=x) for x in range(10)]) - assert as_variable(dts, 'time').dtype.kind == 'M' + # test datetime, timedelta conversion + dt = np.array([datetime(1999, 1, 1) + timedelta(days=x) + for x in range(10)]) + assert as_variable(dt, 'time').dtype.kind == 'M' + td = np.array([timedelta(days=x) for x in range(10)]) + assert as_variable(td, 'time').dtype.kind == 'm' def test_repr(self): v = Variable(['time', 'x'], [[1, 2, 3], [4, 5, 6]], {'foo': 'bar'}) From 217a36fc098aa0cc525b238544949040be5861d5 Mon Sep 17 00:00:00 2001 From: dcherian Date: Fri, 26 Oct 2018 15:58:04 -0700 Subject: [PATCH 3/3] Add whats-new. --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6512d48d7d8..f0cf8008c43 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -86,6 +86,9 @@ Bug fixes - Addition and subtraction operators used with a CFTimeIndex now preserve the index's type. (:issue:`2244`). By `Spencer Clark `_. +- We now properly handle arrays of ``datetime.datetime`` and ``datetime.timedelta`` + provided as coordinates. (:issue:`2512`) + By `Deepak Cherian `_.