仅在两个时间维度中的一个上重新采样DataArray

时间:2018-03-27 17:52:46

标签: python pandas python-xarray

问题我有一个带有两个时间维度的DataArray(初始化时间和预测时间。当我尝试重新采样一次时间变暗时,它会在另一时间完全变暗。

工作示例:

import xarray as xr
import numpy as np
import pandas as pd

# Make two time dims (one datetime64, one timedelta64)
T1 = pd.date_range('2001-01-01','2001-01-28', freq='12H')
T2 = pd.timedelta_range(start='1 day', end='3 day', freq='3H')
da_3H = xr.DataArray(np.random.randn(T1.size, T2.size), dims=('T1','T2'), coords={'T1':T1, 'T2':T2})
print('Original data')
print(da_3H)
print('')

# Resample only the T2 (timedelta) dim from 3H to Daily
da_1D = da_3H.resample(T2='D').mean()
print('Resampled data')
print(da_1D)

输出

Original data
<xarray.DataArray (T1: 55, T2: 17)>
array([[ 1.274925,  1.708316,  0.664372, ...,  0.332091, -0.281352, -0.17172 ],
       [ 0.909488, -0.291555, -0.237311, ..., -0.380613,  0.02045 ,  0.675155],
       [ 0.680156,  0.56136 ,  1.940486, ...,  0.622956,  1.785852,  1.2213  ],
       ...,
       [-1.371333,  0.794905, -2.051659, ..., -0.024027,  0.597598,  1.999001],
       [-1.185423,  1.908118,  0.630214, ..., -0.546125,  1.434227,  0.408871],
       [ 1.522956,  0.987754,  0.116632, ..., -0.110051, -0.652195, -1.057543]])
Coordinates:
  * T1       (T1) datetime64[ns] 2001-01-01 2001-01-01T12:00:00 2001-01-02 ...
  * T2       (T2) timedelta64[ns] 1 days 00:00:00 1 days 03:00:00 ...

Resampled data
<xarray.DataArray (T2: 3)>
array([ 0.023398, -0.00614 ,  0.265382])
Coordinates:
  * T2       (T2) timedelta64[ns] 1 days 2 days 3 days

预期输出:

我预计它只会沿T2暗淡重新取样,让T1暗淡不变。

输出xr.show_versions()

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.12
machine: x86_64
processor: 
byteorder: little
LC_ALL: C
LANG: C
LOCALE: None.None

xarray: 0.10.2
pandas: 0.22.0
numpy: 1.14.1
scipy: 1.0.0
netCDF4: 1.3.1
h5netcdf: 0.5.0
h5py: 2.7.1
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.17.1
distributed: 1.21.1
matplotlib: 2.2.2
cartopy: 0.16.0
seaborn: 0.8.1
setuptools: 38.5.1
pip: 9.0.1
conda: None
pytest: None
IPython: 6.2.1
sphinx: None

1 个答案:

答案 0 :(得分:0)

哦,我需要在mean()函数中指定dim:

da_1D = da_3H.resample(T2='D').mean(dim='T2')

我在read the docs中并不清楚这一点。上述行为是否不包括预期的暗淡?

Resampled data
<xarray.DataArray (T1: 55, T2: 3)>
array([[-0.026289, -0.129011, -1.419823],
       ....
       [ 0.215543, -0.120174, -0.407321],
       [-0.281218,  0.361285,  1.456381],
       [ 0.588086,  0.203041, -1.947097]])
Coordinates:
  * T2       (T2) timedelta64[ns] 1 days 2 days 3 days
  * T1       (T1) datetime64[ns] 2001-01-01 2001-01-01T12:00:00 2001-01-02 ...       [-0.073859,  0.351663, -1.926099],