python - xarray:将时间片“插入”到数据集或数据数组中的最佳方法
问题描述
我有一个维度为、和的 3 维xarray
数据集。假设我知道在 timestep 处缺少观察,那么插入没有数据值的时间片的最佳方法是什么?x
y
time
n
这是一个工作示例:
import xarray as xr
import pandas as pd
x = xr.tutorial.load_dataset("air_temperature")
# assuming this is the missing point in time (currently not in the dataset)
missing = "2014-12-31T07:00:00"
# create an "empty" time slice with fillvalues
empty = xr.full_like(x.isel(time=0), -3000)
# fix the time coordinate of the timeslice
empty['time'] = pd.date_range(missing, periods=1)[0]
# before insertion
print(x.time[-5:].values)
# '2014-12-30T18:00:00.000000000' '2014-12-31T00:00:00.000000000'
# '2014-12-31T06:00:00.000000000' '2014-12-31T12:00:00.000000000'
# '2014-12-31T18:00:00.000000000']
# concat and sort time
x2 = xr.concat([x, empty], "time").sortby("time")
# after insertion
print(x2.time[-5:].values)
# ['2014-12-31T00:00:00.000000000' '2014-12-31T06:00:00.000000000'
# '2014-12-31T07:00:00.000000000' '2014-12-31T12:00:00.000000000'
# '2014-12-31T18:00:00.000000000']
该示例运行良好,但我不确定这是否是最好的(甚至是正确的)方法。
我的担心是将其用于更大的数据集,特别是 dask-array 支持的数据集。
有没有更好的方法来填充缺失的二维数组?插入支持 dask 的数据集时,使用支持 dask 的“填充数组”会更好吗?
解决方案
You might consider using xarray's reindex
method with a constant fill_value
for this purpose:
import numpy as np
import xarray as xr
x = xr.tutorial.load_dataset("air_temperature")
missing_time = np.datetime64("2014-12-31T07:00:00")
missing_time_da = xr.DataArray([missing_time], dims=["time"], coords=[[missing_time]])
full_time = xr.concat([x.time, missing_time_da], dim="time")
full = x.reindex(time=full_time, fill_value=-3000.0).sortby("time")
I think both your method and the reindex
method will automatically use dask-backed arrays if x
is dask-backed.
推荐阅读
- conda - conda:“找不到命令”错误但包存在?
- swift - MacCatalyst 上的核心数据 CloudKit 同步问题
- vba - 如何解决 MS Access 中的错误 - VBA SQL 执行?
- c - 从另一个双指针中减去一个双指针的值是什么意思?
- powerquery - 根据不同的选择,特定列的双重数据类型?
- reactjs - webpack cli什么时候开始发挥作用
- python - 如何在python中使用精度计算浮点数
- python - 如何在 nn.LSTM pytorch 中获得 R2 分数
- swift - 无法从其他应用打开 tvOS 上的通用链接
- javascript - 将值传递给承诺中的另一个变量不起作用