Skip to content

Commit

Permalink
Fix indexing NaT scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
shoyer committed Sep 23, 2014
1 parent 97db2b7 commit 42964a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion xray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ def __getitem__(self, key):
key, = key

if isinstance(key, (int, np.integer)):
value = np.asarray(self.array[key], dtype=self.dtype)
value = self.array[key]
if value is pd.NaT:
# work around the impossibility of casting NaT with asarray
# note: it probably would be better in general to return
# pd.Timestamp rather np.than datetime64 but this is easier
# (for now)
value = np.datetime64('NaT', 'ns')
else:
value = np.asarray(value, dtype=self.dtype)
else:
arr = self.array[key]
if arr.dtype != self.array.dtype:
Expand Down
5 changes: 5 additions & 0 deletions xray/test/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def test_index_0d_datetime(self):
x = self.cls(['x'], pd.DatetimeIndex([d]))
self.assertIndexedLikeNDArray(x, np.datetime64(d), 'datetime64[ns]')

def test_index_0d_not_a_time(self):
d = np.datetime64('NaT')
x = self.cls(['x'], [d])
self.assertIndexedLikeNDArray(x, d, None)

def test_index_0d_object(self):

class HashableItemWrapper(object):
Expand Down

0 comments on commit 42964a3

Please sign in to comment.