xarray.dataset.sel返回不需要的值

时间:2018-07-12 11:00:10

标签: python pandas python-xarray

嗯,这很奇怪。我期望只获得第一个值,而不是一秒钟间隔内的所有值。我很想听听如何只选择所需的点...

>>> wind.W_C.sel(Time = '2017-08-19T10:41:02.000000000', tolerance=0.0000000001).Time
<xarray.DataArray 'Time' (Time: 32)>
array(['2017-08-19T10:41:02.000000000', '2017-08-19T10:41:02.031250000',
   '2017-08-19T10:41:02.062500000', '2017-08-19T10:41:02.093750000',
   '2017-08-19T10:41:02.125000000', '2017-08-19T10:41:02.156250000',
   '2017-08-19T10:41:02.187500000', '2017-08-19T10:41:02.218750000',
   '2017-08-19T10:41:02.250000000', '2017-08-19T10:41:02.281250000',
   '2017-08-19T10:41:02.312500000', '2017-08-19T10:41:02.343750000',
   '2017-08-19T10:41:02.375000000', '2017-08-19T10:41:02.406250000',
   '2017-08-19T10:41:02.437500000', '2017-08-19T10:41:02.468750000',
   '2017-08-19T10:41:02.500000000', '2017-08-19T10:41:02.531250000',
   '2017-08-19T10:41:02.562500000', '2017-08-19T10:41:02.593750000',
   '2017-08-19T10:41:02.625000000', '2017-08-19T10:41:02.656250000',
   '2017-08-19T10:41:02.687500000', '2017-08-19T10:41:02.718750000',
   '2017-08-19T10:41:02.750000000', '2017-08-19T10:41:02.781250000',
   '2017-08-19T10:41:02.812500000', '2017-08-19T10:41:02.843750000',
   '2017-08-19T10:41:02.875000000', '2017-08-19T10:41:02.906250000',
   '2017-08-19T10:41:02.937500000', '2017-08-19T10:41:02.968750000'], 
dtype='datetime64[ns]')
Coordinates:
  * Time     (Time) datetime64[ns] 2017-08-19T10:41:02 ...
>>>

编辑:它对于不完全是x.0000000的任何时间值都可以正常工作(例如,使用时间='2017-08-19T10:41:02.062500000'返回一个点)。

编辑2:切片执行相同的操作:

>>> wind.W_C.sel(Time = slice('2017-08-19T10:41:01.999999999','2017-08-19T10:41:02.000000001'))
<xarray.DataArray 'W_C' (Time: 32)>
array([-0.365306, -0.351855, -0.301889, -0.315359, -0.340766, -0.366259,
   -0.361642, -0.318217, -0.346406, -0.310716, -0.188696, -0.206533,
   -0.228649, -0.219716, -0.259015, -0.348025, -0.251485, -0.251169,
   -0.29893 , -0.419135, -0.397326, -0.329589, -0.325526, -0.286838,
   -0.295504, -0.242547, -0.294295, -0.274641, -0.299403, -0.25828 ,
   -0.16487 , -0.317597], dtype=float32)
Coordinates:
  * Time     (Time) datetime64[ns] 2017-08-19T10:41:02 ...
>>>

编辑3:必须是xarray中的错误。甚至在指定公差时都要小心:

>>> wind.W_C.sel(Time = '2017-08-19T10:41:02.0', tolerance = np.timedelta64(1,'us')).Time
<xarray.DataArray 'Time' (Time: 32)>
array(['2017-08-19T10:41:02.000000000', '2017-08-19T10:41:02.031250000',
   '2017-08-19T10:41:02.062500000', '2017-08-19T10:41:02.093750000',
   '2017-08-19T10:41:02.125000000', '2017-08-19T10:41:02.156250000',
   '2017-08-19T10:41:02.187500000', '2017-08-19T10:41:02.218750000',
   '2017-08-19T10:41:02.250000000', '2017-08-19T10:41:02.281250000',
   '2017-08-19T10:41:02.312500000', '2017-08-19T10:41:02.343750000',
   '2017-08-19T10:41:02.375000000', '2017-08-19T10:41:02.406250000',
   '2017-08-19T10:41:02.437500000', '2017-08-19T10:41:02.468750000',
   '2017-08-19T10:41:02.500000000', '2017-08-19T10:41:02.531250000',
   '2017-08-19T10:41:02.562500000', '2017-08-19T10:41:02.593750000',
   '2017-08-19T10:41:02.625000000', '2017-08-19T10:41:02.656250000',
   '2017-08-19T10:41:02.687500000', '2017-08-19T10:41:02.718750000',
   '2017-08-19T10:41:02.750000000', '2017-08-19T10:41:02.781250000',
   '2017-08-19T10:41:02.812500000', '2017-08-19T10:41:02.843750000',
   '2017-08-19T10:41:02.875000000', '2017-08-19T10:41:02.906250000',
   '2017-08-19T10:41:02.937500000', '2017-08-19T10:41:02.968750000'], dtype='datetime64[ns]')
Coordinates:
  * Time     (Time) datetime64[ns] 2017-08-19T10:41:02 ...
>>> 

最终编辑:似乎与xarray解码datetime64字符串的方式有关吗?这有效:

>>> wind.W_C.sel(Time = np.datetime64('2017-08-19T10:41:02.000000000')).Time
<xarray.DataArray 'Time' ()>
array(1503139262000000000L, dtype='datetime64[ns]')
Coordinates:
    Time     datetime64[ns] 2017-08-19T10:41:02
>>>

0 个答案:

没有答案