我想用这个列表为我的变量添加维度:
['Time', 'Pressure', 'Temperature', 'Electrical conductivity', 'Salinity', 'Chlorophyll concentration']
。
所以我试着列一个尺寸列表,但没有用。
ds = xr.Dataset(
coords = coords,
data_vars = {"hehe" : (('Time', 'Pres'), col_names_values2['c731'])},
attrs = global_attributes
)
当我执行我的代码时,我得到这个:
ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): (('Time', 'Pres'), [55, 22]) to Variable.
另一方面,当我只在括号之间放一个时,一切顺利:
<xarray.Dataset>
Dimensions: (Time: 55, Pres: 55, Temp: 55, Cond: 55, Sal: 55,
CHL: 55)
Coordinates:
* Time (Time) int32 55
* Pres (Pres) float64 55
* Temp (Temp) float64 55
* Cond (Cond) float64 55
* Sal (Sal) float64 55
* CHL (CHL) float64 55
Data variables:
hehe (Time) float64 55
毕竟,我试图在 xarray 文档上找到一个例子,所以我尝试了我找到的东西并且它有效!问题是我以前写过完全相同的东西,形式也一样,但它不起作用。这是我找到的代码:
ds = xr.Dataset(
coords = coords,
data_vars = {"foo": (("x", "y"), np.random.rand(4, 5))},
attrs = global_attributes
)
这是结果:
<xarray.Dataset>
Dimensions: (x: 4, y: 5, Time: 55, Pres: 55, Temp: 55, Cond: 55,
Sal: 55, chl: 55)
Coordinates:
* Time (Time) 55
* Pres (Pres) 55
* Temp (Temp) 55
* Cond (Cond) 55
* Sal (Sal) 55
* CHL (CHL) 55
Dimensions without coordinates: x, y
Data variables:
foo (x, y) float64 55
有人对此有解决方案吗?
在我看来
col_names_values2['c731']
在你的情况下是一维的,即使你告诉它是时间和压力的函数。
我可以重现你的错误:
import xarray as xr
import numpy as np
import pandas as pd
coords = dict(
Time=pd.date_range("2000-01-01", periods=11, freq="1d"),
Pres=np.logspace(start=3, stop=0, base=10, num=5),
)
some_data = [
33.12143542898356,
33.12143542898356,
33.12143542898356,
33.12143542898356,
33.12120589409984,
33.18208891735566,
33.12825967505915,
33.12825967505915,
33.12825967505915,
33.12825967505915,
33.12825967505915,
]
xr.Dataset(coords=coords, data_vars={"hehe": (("Time", "Pres"), some_data)})
这失败了:
ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): (('Time', 'Pres'), [33.12143542898356, 33.12143542898356, 33.12143542898356, 33.12143542898356, 33.12120589409984, 33.18208891735566, 33.12825967505915, 33.12825967505915, 33.12825967505915, 33.12825967505915, 33.12825967505915]) to Variable.
然而,传递实际的 2D 数据是有效的:
ntime = len(coords["Time"])
npres = len(coords["Pres"])
some_better_data = np.random.normal(size=(ntime, npres))
ds = xr.Dataset(coords=coords, data_vars={"hehe": (("Time", "Pres"), some_better_data)})
这会产生以下
ds
:
<xarray.Dataset>
Dimensions: (Time: 20, Pres: 5)
Coordinates:
* Time (Time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-20
* Pres (Pres) float64 1e+03 177.8 31.62 5.623 1.0
Data variables:
hehe (Time, Pres) float64 -1.053 -0.5254 0.6547 ... -0.6255 -0.2138
您遇到的问题是由于没有正确指定数据变量“hehe”的维度。维度应指定为一个元组,其中包含维度名称及其对应的大小,按照它们在数据中出现的顺序排列。在你的例子中,“hehe”的正确尺寸应该是(“Time”,“Pres”)。
我没有你的原始数据,所以这里有一个例子:
import pandas as pd
import xarray as xr
import numpy as np
time = np.arange(0, 365, 1)
time = pd.date_range(start='2000-01-01', periods=len(time), freq='D')
pressure = np.linspace(0, 1000, 20)
temperature = np.random.rand(len(time), len(pressure))
conductivity = np.random.rand(len(time), len(pressure))
salinity = np.random.rand(len(time), len(pressure))
chlorophyll = np.random.rand(len(time), len(pressure))
ds = xr.Dataset(
data_vars={
'temperature': (('time', 'pressure'), temperature),
'conductivity': (('time', 'pressure'), conductivity),
'salinity': (('time', 'pressure'), salinity),
'chlorophyll': (('time', 'pressure'), chlorophyll),
},
coords={
'time': time,
'pressure': pressure,
},
attrs={
'description': 'Example dataset for testing dimensions',
'author': 'Your name',
},
)
产生
你可以向下钻取