In [1]:
import xarray as xr
import numpy as np
import pandas as pd

In [2]:
xr.show_versions()


INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

xarray: 0.10.3
pandas: 0.19.0
numpy: 1.11.3
scipy: 1.1.0
netCDF4: 1.2.2
h5netcdf: 0.2.2
h5py: 2.6.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: 1.0.0
dask: 0.14.1
distributed: 1.21.8
matplotlib: 2.2.2
cartopy: None
seaborn: 0.8.1
setuptools: 27.2.0.post20161106
pip: 9.0.1
conda: None
pytest: 3.6.0
IPython: 6.4.0
sphinx: None

In [3]:
data = np.random.rand(4, 90, 180)

In [4]:
lat = np.arange(0,90,1)

In [5]:
lon = np.arange(0,180,1)

In [6]:
time = pd.date_range('2000-01-01T12:00:00', periods=4)

In [7]:
foo = xr.DataArray(data, coords=[time, lat, lon], dims=['time', 'lat', 'lon'])

In [8]:
foo


Out[8]:
<xarray.DataArray (time: 4, lat: 90, lon: 180)>
array([[[ 0.332231,  0.923876, ...,  0.916489,  0.511909],
        [ 0.660899,  0.59784 , ...,  0.310555,  0.707859],
        ..., 
        [ 0.938327,  0.170786, ...,  0.762403,  0.252795],
        [ 0.221041,  0.132933, ...,  0.056459,  0.337369]],

       [[ 0.191474,  0.442844, ...,  0.114375,  0.197824],
        [ 0.156741,  0.400949, ...,  0.390564,  0.586544],
        ..., 
        [ 0.654393,  0.563333, ...,  0.839175,  0.554296],
        [ 0.42146 ,  0.409053, ...,  0.774658,  0.277885]],

       [[ 0.660065,  0.398647, ...,  0.799548,  0.315824],
        [ 0.316837,  0.999993, ...,  0.745206,  0.111975],
        ..., 
        [ 0.875542,  0.361742, ...,  0.580985,  0.930637],
        [ 0.560711,  0.714933, ...,  0.232878,  0.707307]],

       [[ 0.893143,  0.807658, ...,  0.00173 ,  0.112017],
        [ 0.440732,  0.701912, ...,  0.359352,  0.69418 ],
        ..., 
        [ 0.183569,  0.340203, ...,  0.263483,  0.872148],
        [ 0.624768,  0.407723, ...,  0.211212,  0.433252]]])
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01T12:00:00 2000-01-02T12:00:00 ...
  * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
  * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

In [9]:
foo.sel(time='2000-01-01T12:00:00', method = 'nearest')


Out[9]:
<xarray.DataArray (lat: 90, lon: 180)>
array([[ 0.332231,  0.923876,  0.855307, ...,  0.996365,  0.916489,  0.511909],
       [ 0.660899,  0.59784 ,  0.28138 , ...,  0.377135,  0.310555,  0.707859],
       [ 0.913347,  0.977776,  0.995024, ...,  0.469911,  0.8456  ,  0.971022],
       ..., 
       [ 0.947261,  0.76744 ,  0.019311, ...,  0.584562,  0.789416,  0.882229],
       [ 0.938327,  0.170786,  0.105226, ...,  0.124691,  0.762403,  0.252795],
       [ 0.221041,  0.132933,  0.008797, ...,  0.026372,  0.056459,  0.337369]])
Coordinates:
    time     datetime64[ns] 2000-01-01T12:00:00
  * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
  * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

In [10]:
foo.sel(time='2000-01-01', method = 'nearest')


Out[10]:
<xarray.DataArray (time: 1, lat: 90, lon: 180)>
array([[[ 0.332231,  0.923876, ...,  0.916489,  0.511909],
        [ 0.660899,  0.59784 , ...,  0.310555,  0.707859],
        ..., 
        [ 0.938327,  0.170786, ...,  0.762403,  0.252795],
        [ 0.221041,  0.132933, ...,  0.056459,  0.337369]]])
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01T12:00:00
  * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
  * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

In [ ]: