Neither Quantity object nor its magnitude supports indexing

简介: Neither Quantity object nor its magnitude supports indexing

Issue



python 处理气象数据过程中,使用metpy的units 给变量附上单位并进行相关求偏导,循环计算时,出现报错:Neither Quantity object nor its magnitude supports indexing


发现问题来源:对变量赋上单位,求完梯度之后还是array格式,而不是pint.quantity.build_quantity_class.<locals>.Quantity


问题解决:

上述问题在于,提取变量赋上单位时,需要提供的是变量的实际values,而不是dataarray等其他格式。


所以,只需要在提取变量时在其后面加上.data再进行赋单位即可,举个例子如下所示:


import xarray as xr
f_w = r'D:/PV/omega_smooth3.nc'
da  = xr.open_dataset(f_w)
omega = da.omega.data.units('Pa/s')


这样,就可以成功解决循环时的报错问题啦

相关文章
|
6月前
ERROR: No matching distribution found for tb-nightly
ERROR: No matching distribution found for tb-nightly
195 1
|
6月前
|
Python
完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing
完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
320 0
|
缓存 数据可视化
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
210 0
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)
122 0
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(2)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(2)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(2)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(1)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(1)
SAP WM Stock Removal Strategy M (Small/Large Quantity)(1)
成功解决KeyError: “Passing list-likes to .loc or [] with any missing labels is no longer supported. The
成功解决KeyError: “Passing list-likes to .loc or [] with any missing labels is no longer supported. The
|
供应链
PAT (Advanced Level) Practice - 1079 Total Sales of Supply Chain(25 分)
PAT (Advanced Level) Practice - 1079 Total Sales of Supply Chain(25 分)
138 0
|
算法 前端开发 弹性计算
译《Time, Clocks, and the Ordering of Events in a Distributed System》
Motivation 《Time, Clocks, and the Ordering of Events in a Distributed System》大概是在分布式领域被引用的最多的一篇Paper了。
902 0