[英]Pickling xarray gives cannot pickle '_thread.lock' object
I am running into some problem with pickling an xarray and totally fail to understand what the problem is.我在对 xarray 进行酸洗时遇到了一些问题,完全不明白问题是什么。
import xarray as xr
import pickle
ds = xr.open_dataset('62030_prec_1995.nc')
pkl = pickle.dumps(ds, protocol=-1)
This results in the error message:这会导致错误消息:
Traceback (most recent call last):
File "C:\Users\xxx\Miniconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3441, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-08c3b812c30c>", line 1, in <module>
pkl = pickle.dumps(ds, protocol=-1)
TypeError: cannot pickle '_thread.lock' object
If I run ds.info() it gives me the following info on the file:如果我运行 ds.info() 它会为我提供有关文件的以下信息:
ds.info()
xarray.Dataset {
dimensions:
time = 365 ;
bnds = 2 ;
rlon = 848 ;
rlat = 824 ;
variables:
datetime64[ns] time(time) ;
time:standard_name = time ;
time:bounds = time_bnds ;
time:axis = T ;
datetime64[ns] time_bnds(time, bnds) ;
float64 rlon(rlon) ;
rlon:standard_name = projection_x_coordinate ;
rlon:long_name = longitude in rotated pole grid ;
rlon:units = degrees ;
rlon:axis = X ;
float64 rlat(rlat) ;
rlat:standard_name = projection_y_coordinate ;
rlat:long_name = latitude in rotated pole grid ;
rlat:units = degrees ;
rlat:axis = Y ;
int32 rotated_pole() ;
rotated_pole:grid_mapping_name = rotated_latitude_longitude ;
rotated_pole:grid_north_pole_longitude = -162.0 ;
rotated_pole:grid_north_pole_latitude = 39.25 ;
float32 prec(time, rlat, rlon) ;
prec:code = 260 ;
prec:table = 2 ;
prec:grid_mapping = rotated_pole ;
prec:cell_methods = time: mean ;
// global attributes:
:CDI = Climate Data Interface version 1.9.10 (https://mpimet.mpg.de/cdi) ;
:Conventions = CF-1.6 ;
}
I had the same problem, and I solved it by installing the dask
package (with pip install dask
).我遇到了同样的问题,我通过安装
dask
package (使用pip install dask
)解决了这个问题。 Unfortunately I do not have a detailed explanation about why, but since xarray
uses dask
under the hood I guess it is also needed for correctly pickling objects.不幸的是,我没有关于原因的详细解释,但由于
xarray
在引擎盖下使用了dask
,我猜它也是正确酸洗对象所必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.