对多维数组应用 Mann Whitney U 检验并替换 Python 中 xarray 数据数组变量的单个值?

Apply Mann Whitney U test on multidimensional array and replace single values of variable of xarray data array in Python?

我是 Python 的新手,需要一些 xarray 方面的帮助。 我有两个用于未来和过去气候的 3 维数据阵列(rlon、rlat、时间)。我想为每个网格点计算 Mann-Whitney-U 检验,以分析未来与过去相比温度变化的显着性。我已经得到了 Mann-Whitney-U-test 的工作,分别从一个历史和未来数据的网格点中选择一个时间序列。示例:

import numpy as np
import xarray as xr
import scipy.stats as sts

#selecting time period and grid point of past and future data

tp = fileHis['tas']
tf = fileFut['tas']
gridpoint_past=tp.sel(rlon=-6.375, rlat=1.375, time=slice('1999-01-01', '1999-01-31'))
gridpoint_future=tf.sel(rlon=-6.375, rlat=1.375, time=slice('2099-01-01', '2099-01-31'))

#mannwhintey-u-test

result=sts.mannwhitneyu(gridpoint_past, gridpoint_future, alternative='two-sided')
print('pvalue =',result[1])

输出: pvalue = 0.05922372345359562

我现在的问题是我需要为每个网格点和每个月执行此操作,最后我想要一个数据数组,每个网格点和一年中的每个月都有 pvalues。 我正在考虑循环遍历所有 rlat、rlon 和月份以及 运行 每个的 Mann-Whitney-U-test,除非有更好的方法。? 以及如何将 pvalues 一个一个地写入具有相同 rlat、rlon 维度的新数据数组? 我正在尝试这个,但它不起作用: 我创建了一个数据数组 pvalue_mon,它具有与 tptf 相同的 rlat、rlon,并且具有 12 个月的时间步长。

pvalue_mon.sel(rlon=-6.375, rlat=1.375, time=th.time.dt.month.isin([1])) = result[1] 
SyntaxError: can't assign to function call

或者这个:

pvalue_mon.sel(rlon=-6.375, rlat=1.375, time=pvalue_mon.time.dt.month.isin([1])).update(result[1])
TypeError: 'numpy.float64' object is not iterable

如何替换现有变量的单个值?

不使用 .sel() 函数,而是尝试使用 .loc[ ],如下所述: http://xarray.pydata.org/en/stable/indexing.html#assigning-values-with-indexing