# Buggy code
def __getitem__(self, indexer):
    key = indexer.tuple
    if isinstance(key, tuple) and len(key) == 1:
        # unpack key so it can index a pandas.Index object (pandas.Index
        # objects don't like tuples)
        (key,) = key

    if getattr(key, "ndim", 0) > 1:  # Return np-array if multidimensional
        return NumpyIndexingAdapter(self.array.values)[indexer]

    result = self.array[key]

    if isinstance(result, pd.Index):
        result = PandasIndexAdapter(result, dtype=self.dtype)
    else:
        # result is a scalar
        if result 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)
            result = np.datetime64("NaT", "ns")
        elif isinstance(result, timedelta):
            result = np.timedelta64(getattr(result, "value", result), "ns")
        elif self.dtype != object:
            result = np.asarray(result, dtype=self.dtype)

        # as for numpy.ndarray indexing, we always want the result to be
        # a NumPy array.
        result = utils.to_0d_array(result)

    return result


# Traceback (KeyError)
In [7]: ds.sel(time=ds.time[1])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-371d2f896b4a> in <module>()
----> 1 ds.sel(time=ds.time[1])

/usr/lib/python2.7/site-packages/xarray/core/dataset.pyc in sel(self, method, tolerance, drop, **indexers)
1444
1445         pos_indexers, new_indexes = indexing.remap_label_indexers(
-> 1446             self, v_indexers, method=method, tolerance=tolerance
1447         )
1448         # attach indexer's coordinate to pos_indexers

/usr/lib/python2.7/site-packages/xarray/core/indexing.pyc in remap_label_indexers(data_obj, indexers, method, tolerance)
234         else:
235             idxr, new_idx = convert_label_indexer(index, label,
--> 236                                                   dim, method, tolerance)
237             pos_indexers[dim] = idxr
238             if new_idx is not None:

/usr/lib/python2.7/site-packages/xarray/core/indexing.pyc in convert_label_indexer(index, label, index_name, method, tolerance)
163                 indexer, new_index = index.get_loc_level(label.item(), level=0)
164             else:
--> 165                 indexer = get_loc(index, label.item(), method, tolerance)
166         elif label.dtype.kind == 'b':
167             indexer = label

/usr/lib/python2.7/site-packages/xarray/core/indexing.pyc in get_loc(index, label, method, tolerance)
93 def get_loc(index, label, method=None, tolerance=None):
94     kwargs = _index_method_kwargs(method, tolerance)
---> 95     return index.get_loc(label, **kwargs)
96
97

/usr/lib/python2.7/site-packages/pandas/core/indexes/datetimes.pyc in get_loc(self, key, method, tolerance)
1444                 return Index.get_loc(self, stamp, method, tolerance)
1445             except KeyError:
-> 1446                 raise KeyError(key)
1447             except ValueError as e:
1448                 # list-like tolerance size must match target index size

KeyError: 1518418799999986000L

# Fixed code
def __getitem__(self, indexer):
    key = indexer.tuple
    if isinstance(key, tuple) and len(key) == 1:
        # unpack key so it can index a pandas.Index object (pandas.Index
        # objects don't like tuples)
        (key,) = key

    if getattr(key, "ndim", 0) > 1:  # Return np-array if multidimensional
        return NumpyIndexingAdapter(self.array.values)[indexer]

    result = self.array[key]

    if isinstance(result, pd.Index):
        result = PandasIndexAdapter(result, dtype=self.dtype)
    else:
        # result is a scalar
        if result 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)
            result = np.datetime64("NaT", "ns")
        elif isinstance(result, timedelta):
            result = np.timedelta64(getattr(result, "value", result), "ns")
        elif isinstance(result, pd.Timestamp):
            # Work around for GH: pydata/xarray#1932 and numpy/numpy#10668
            # numpy fails to convert pd.Timestamp to np.datetime64[ns]
            result = np.asarray(result.to_datetime64())
        elif self.dtype != object:
            result = np.asarray(result, dtype=self.dtype)

        # as for numpy.ndarray indexing, we always want the result to be
        # a NumPy array.
        result = utils.to_0d_array(result)

    return result