Skip to content

Commit 2f0096c

Browse files
authored
Make sure datetime object arrays are converted to datetime64 (#2513)
Fixes #2512
1 parent b622c5e commit 2f0096c

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Bug fixes
9494
- Addition and subtraction operators used with a CFTimeIndex now preserve the
9595
index's type. (:issue:`2244`).
9696
By `Spencer Clark <https://github.com/spencerkclark>`_.
97+
- We now properly handle arrays of ``datetime.datetime`` and ``datetime.timedelta``
98+
provided as coordinates. (:issue:`2512`)
99+
By `Deepak Cherian <https://github.com/dcherian`_.
97100
- ``xarray.DataArray.roll`` correctly handles multidimensional arrays.
98101
(:issue:`2445`)
99102
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

xarray/core/variable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def as_variable(obj, name=None):
9595
'cannot set variable %r with %r-dimensional data '
9696
'without explicit dimension names. Pass a tuple of '
9797
'(dims, data) instead.' % (name, data.ndim))
98-
obj = Variable(name, obj, fastpath=True)
98+
obj = Variable(name, data, fastpath=True)
9999
else:
100100
raise TypeError('unable to convert object into a variable without an '
101101
'explicit list of dimensions: %r' % obj)

xarray/tests/test_variable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,13 @@ def test_as_variable(self):
992992
ValueError, 'has more than 1-dimension'):
993993
as_variable(expected, name='x')
994994

995+
# test datetime, timedelta conversion
996+
dt = np.array([datetime(1999, 1, 1) + timedelta(days=x)
997+
for x in range(10)])
998+
assert as_variable(dt, 'time').dtype.kind == 'M'
999+
td = np.array([timedelta(days=x) for x in range(10)])
1000+
assert as_variable(td, 'time').dtype.kind == 'm'
1001+
9951002
def test_repr(self):
9961003
v = Variable(['time', 'x'], [[1, 2, 3], [4, 5, 6]], {'foo': 'bar'})
9971004
expected = dedent("""

0 commit comments

Comments
 (0)