Pandas DF 到 Xarray 数据集

问题描述

嗨,最初我有 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 records(721 * 1440) 和 tas 将具有71 values(71 time) 数组:

latitude    longitude   tas
-90.0        358.75     [50603.53125,50002.609375,50183.98828125,49...
-90.0        359.00     [50603.53125,49...
-90.0        359.25     [50603.53125,49...
-90.0        359.50     [50603.53125,49...
-90.0        359.75     [50603.53125,49...

现在我已经执行了一些操作并创建了新列 tas_new,其大小与 tas 相似。现在我想创建新数据集或将此变量添加到具有相同维度 (time,longitude) 的旧数据集中。但我无法将它改造成旧的。

我尝试从 tas_new 获取所有值并像这样堆叠它们:

array_tuple = (df_groups['trend'].values)
arrays = np.vstack(array_tuple)

这确实返回了形状为 (1038239,71) 的数组。有人可以指导我如何恢复原始形状并将该变量添加到 xarray 数据集或创建新的。

预期结果:

<xarray.Dataset>
Dimensions:    (latitude: 721,longitude) float64 5.033e+05 ... 1.908e+05
    tas_new  (time,longitude) float64 5.033e+05 ... 1.908e+05

或者来自数据帧的维度数组 (time,longitude)

解决方法

所以一旦我有了 arrays = np.vstack(array_tuple),我就将它们转换为整个列表,形状为 (1038239*71),然后添加对应于它的经纬度对的原始数据帧。然后将整个数据帧转换回 xarray。

PS:因此,对于低内存(

如果将来有人想要详细信息或明确的答案,请在此处发表评论,我会尽力使其更准确。