issue_comments: 746446912
This data as json
html_url | issue_url | id | node_id | user | created_at | updated_at | author_association | body | reactions | performed_via_github_app | issue |
---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/pydata/xarray/pull/4700#issuecomment-746446912 | https://api.github.com/repos/pydata/xarray/issues/4700 | 746446912 | MDEyOklzc3VlQ29tbWVudDc0NjQ0NjkxMg== | 13301940 | 2020-12-16T15:11:12Z | 2020-12-16T15:18:18Z | MEMBER | Before```python In [2]: data = np.array([["x", 1], ["y", 2]], dtype="object") In [3]: xr.conventions._infer_dtype(data, 'test') Out[3]: dtype('O') ``` As pointed out in #2620, this doesn't seem problematic until the user tries writing the xarray object to disk. This results in a very cryptic error message: ```python In [7]: ds.to_netcdf('test.nc', engine='netcdf4') netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.setitem() netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable._put() TypeError: expected bytes, int found ``` After```python In [2]: data = np.array([["x", 1], ["y", 2]], dtype="object") In [3]: xr.conventions._infer_dtype(data, 'test')ValueError Traceback (most recent call last) <ipython-input-3-addaab43c03a> in <module> ----> 1 xr.conventions._infer_dtype(data, 'test') ~/devel/pydata/xarray/xarray/conventions.py in _infer_dtype(array, name) 142 native_dtypes = set(map(lambda x: type(x), array.flatten())) 143 if len(native_dtypes) > 1: --> 144 raise ValueError( 145 "unable to infer dtype on variable {!r}; object array " 146 "contains mixed native types: {}".format( ValueError: unable to infer dtype on variable 'test'; object array contains mixed native types: str,int ``` During I/O, the user gets: ```python ... ~/devel/pydata/xarray/xarray/conventions.py in ensure_dtype_not_object(var, name) 223 data[missing] = fill_value 224 else: --> 225 data = _copy_with_dtype(data, dtype=_infer_dtype(data, name)) 226 227 assert data.dtype.kind != "O" or data.dtype.metadata ~/devel/pydata/xarray/xarray/conventions.py in _infer_dtype(array, name) 142 native_dtypes = set(map(lambda x: type(x), array.flatten())) 143 if len(native_dtypes) > 1: --> 144 raise ValueError( 145 "unable to infer dtype on variable {!r}; object array " 146 "contains mixed native types: {}".format( ValueError: unable to infer dtype on variable 'test'; object array contains mixed native types: str,int ``` |
{ "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
768981497 |