您好,最初我有如下 Xarray 数据集:
<xarray.Dataset>
Dimensions: (latitude: 721, longitude: 1400, time: 71)
Coordinates:
* time (time) datetime64[ns] 2000-12-31 2001-12-31 ... 2018-12-31
* longitude (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
* latitude (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
Data variables:
tas (time, latitude, longitude) float64 5.033e+05 ... 1.908e+05
现在我将它转换成数据帧并在 latitude and longitude
上使用 groupby 函数来获取所有时间维度的 tas
值,这就是样本 df 将具有 1038239 条记录(721 * 1440)
tas 将有 71 个值(71 次)的数组
:
latitude longitude tas
-90.0 358.75 [50603.53125, 50002.609375, 50183.98828125, 49...
-90.0 359.00 [50603.53125, 50002.609375, 50183.98828125, 49...
-90.0 359.25 [50603.53125, 50002.609375, 50183.98828125, 49...
-90.0 359.50 [50603.53125, 50002.609375, 50183.98828125, 49...
-90.0 359.75 [50603.53125, 50002.609375, 50183.98828125, 49...
现在我执行了一些操作并创建了新列 tas_new
,其大小与 tas
相似。现在我想创建新数据集或在具有相同维度的旧数据集中添加此变量 (time, latitude, longitude)
。但我无法将它 reshape 回原来的样子。
我尝试从 tas_new
获取所有值并将它们堆叠起来,如下所示:
array_tuple = (df_groups['trend'].values)
arrays = np.vstack(array_tuple)
这确实会返回形状为 (1038239, 71)
的数组。有人可以指导我如何恢复原始形状并将该变量添加到 xarray 数据集或创建新的数据集。
预期结果:
<xarray.Dataset>
Dimensions: (latitude: 721, longitude: 1400, time: 71)
Coordinates:
* time (time) datetime64[ns] 2000-12-31 2001-12-31 ... 2018-12-31
* longitude (longitude) float32 -22.5 -21.75 -21.0 -20.25 ... 43.5 44.25 45.0
* latitude (latitude) float32 72.0 71.25 70.5 69.75 ... 28.5 27.75 27.0
Data variables:
tas (time, latitude, longitude) float64 5.033e+05 ... 1.908e+05
tas_new (time, latitude, longitude) float64 5.033e+05 ... 1.908e+05
或来自数据框的维度(时间,纬度,经度)
数组。
最佳答案
因此,一旦我有了 arrays = np.vstack(array_tuple)
,我就将它们转换为整个列表,形状为 (1038239*71)
,然后添加对应于它的经纬度时间对的原始数据帧。然后将整个数据帧转换回 xarray。
PS:对于低内存(<12GB)系统,数据帧非常庞大,无法转换为 xarray,因此我将数据帧分成 7 个部分,将每个部分转换为 xarray,然后将它们连接起来以获得完整的 Xarray 数据集。
如果以后有人想要详细或明确的答案,请在这里发表评论,我会尽量使其更准确。
关于python - Pandas DF 到 Xarray 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66661392/