首页 > 解决方案 > xarray:将时间片“插入”到数据集或数据数组中的最佳方法

问题描述

我有一个维度为、和的 3 维xarray数据集。假设我知道在 timestep 处缺少观察,那么插入没有数据值的时间片的最佳方法是什么?xytimen

这是一个工作示例:

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 的“填充数组”会更好吗?

标签: pythonpandastime-seriespython-xarray

解决方案


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.


推荐阅读