我有一个xarray数据集,其中包含三个单独的4x4矩阵,当前填充有随机值。
我可以屏蔽每个4x4矩阵,以便所有等于0的值都是nan,我想用下一个矩阵向下的值替换这些nan值。
最终它将扩展到非常大的卫星图像阵列,以便我可以执行搜索并根据“最后的最佳像素”创建图像。以下是我当前用于参考的代码:
host.0: "m"
host.1: "a"
host.2: "x"
information.creation_utctime: "1494831805258"
information.data_id: "11138"
information.data_size: 186497
information.type: "image/jpeg"
roll.0: "1"
roll.1: "1"
roll.2: "1"
roll.3: "3"
roll.4: "8"
subhost.0: "s"
subhost.1: "a"
subhost.2: "m"
subhost.3: "p"
subhost.4: "l"
subhost.5: "e"
urls.0: "h"
urls.1: "t"
urls.10: "a"
urls.11: "l"
urls.12: "h"
urls.13: "o"
urls.14: "s"
urls.15: "t"
urls.16: ":"
urls.17: "9"
urls.18: "6"
urls.19: "1"
urls.2: "t"
urls.20: "8"
urls.21: "/"
urls.22: "i"
urls.23: "n"
urls.24: "f"
urls.25: "o"
urls.26: "?"
urls.27: "d"
urls.28: "a"
urls.29: "t"
urls.3: "p"
urls.30: "a"
urls.31: "_"
urls.32: "i"
urls.33: "d"
urls.34: "="
urls.35: "1"
urls.36: "1"
urls.37: "1"
urls.38: "3"
urls.39: "8"
urls.4: ":"
urls.5: "/"
urls.6: "/"
urls.7: "l"
urls.8: "o"
urls.9: "c"
_id: "11138"
我想做的是将b中任何被屏蔽为False的值替换为与“ 2017-10-12”相对应的4x4矩阵中的值。任何帮助,将不胜感激。
答案 0 :(得分:1)
您可以使用ffill()
和bfill()
方法(例如,
import numpy as np
import xarray as xr
dval = np.random.RandomState(0).randint(5,size=[3,4,4])
x = [0,1,2,3]
y = [0,1,2,3]
time = ['2017-10-13','2017-10-12','2017-10-11']
a = xr.DataArray(dval,coords=[time,x,y],dims=['time','x','y'])
a = a.where(a > 0)
filled = a.bfill('time')
结果:
>>> a
<xarray.DataArray (time: 3, x: 4, y: 4)>
array([[[ 4., nan, 3., 3.],
[ 3., 1., 3., 2.],
[ 4., nan, nan, 4.],
[ 2., 1., nan, 1.]],
[[ 1., nan, 1., 4.],
[ 3., nan, 3., nan],
[ 2., 3., nan, 1.],
[ 3., 3., 3., nan]],
[[ 1., 1., 1., nan],
[ 2., 4., 3., 3.],
[ 2., 4., 2., nan],
[nan, 4., nan, 4.]]])
Coordinates:
* time (time) <U10 '2017-10-13' '2017-10-12' '2017-10-11'
* x (x) int64 0 1 2 3
* y (y) int64 0 1 2 3
>>> filled
<xarray.DataArray (time: 3, x: 4, y: 4)>
array([[[ 4., 1., 3., 3.],
[ 3., 1., 3., 2.],
[ 4., 3., 2., 4.],
[ 2., 1., 3., 1.]],
[[ 1., 1., 1., 4.],
[ 3., 4., 3., 3.],
[ 2., 3., 2., 1.],
[ 3., 3., 3., 4.]],
[[ 1., 1., 1., nan],
[ 2., 4., 3., 3.],
[ 2., 4., 2., nan],
[nan, 4., nan, 4.]]])
Coordinates:
* time (time) <U10 '2017-10-13' '2017-10-12' '2017-10-11'
* x (x) int64 0 1 2 3
* y (y) int64 0 1 2 3
相关的interpolate_na()
方法在这些情况下也很方便(但在这种情况下不是这样)。