Uh oh!
There was an error while loading. Please reload this page.
Numpy string coding - #5264
Conversation
znicholls
commented
May 6, 2021
So far I've just added a single test which fails. I don't think the test should fail although I'm not sure what the |
shoyer
commented
May 6, 2021
What problem are you trying to solve here? This vlen string stuff is an internal API that isn't really intended for use outside Xarray. |
Somehow I ended up with importnumpyasnpimportpandasaspd# I don't know how the strings ended up being np.str_....scenarios= [np.str_(v) forvin ["scenario_a", "scenario_b", "scenario_c"]]
years=range(2015, 2100+1)
tdf=pd.DataFrame(
data=np.random.random((len(scenarios), len(years))),
columns=years,
index=scenarios,
)
tdf.index.name="scenario"tdf.columns.name="year"tdf=tdf.stack()
tdf.name="tas"txr=tdf.to_xarray()
# raises error shown belowtxr.to_netcdf("test.nc")
# errorTraceback (mostrecentcalllast):
File"scratch.py", line20, in<module>txr.to_netcdf("test.nc")
File".../lib/python3.7/site-packages/xarray/core/dataarray.py", line2741, into_netcdfreturndataset.to_netcdf(*args, **kwargs)
File".../lib/python3.7/site-packages/xarray/core/dataset.py", line1699, into_netcdfinvalid_netcdf=invalid_netcdf,
File".../lib/python3.7/site-packages/xarray/backends/api.py", line1108, into_netcdfdataset, store, writer, encoding=encoding, unlimited_dims=unlimited_dimsFile".../lib/python3.7/site-packages/xarray/backends/api.py", line1154, indump_to_storestore.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
File".../lib/python3.7/site-packages/xarray/backends/common.py", line256, instorevariables, check_encoding_set, writer, unlimited_dims=unlimited_dimsFile".../lib/python3.7/site-packages/xarray/backends/common.py", line294, inset_variablesname, v, check, unlimited_dims=unlimited_dimsFile".../lib/python3.7/site-packages/xarray/backends/netCDF4_.py", line464, inprepare_variablevariable, self.format, raise_on_invalid_encoding=check_encodingFile".../lib/python3.7/site-packages/xarray/backends/netCDF4_.py", line131, in_get_datatypedatatype=_nc4_dtype(var)
File".../lib/python3.7/site-packages/xarray/backends/netCDF4_.py", line154, in_nc4_dtyperaiseValueError(f"unsupported dtype for netCDF4 variable: {var.dtype}")
ValueError: unsupporteddtypefornetCDF4variable: object |
znicholls
commented
May 26, 2021
@shoyer any further thoughts on this now that the scope is clearer? |
shoyer
commented
May 26, 2021
I agree, this should totally work. It's not obvious to me how to best fix it, though. |
znicholls
commented
May 26, 2021
I assume it's not as trivial as just changing e.g. xarray/xarray/coding/strings.py Line 32 in f9a535c np.str_? |
shoyer
commented
May 26, 2021
I think the issue must be somewhere around this line, where xarray attempts to infer a dtype for object arrays: Line 215 in f9a535c |
33f0e09 to
ca6abdbCompareznicholls
commented
Jul 14, 2021
I tried pushing a fix. It's unclear to me whether the change should be in how the dtypes are inferred (given that the inference code seems to do what it is meant to...) or whether |
shoyer
commented
Jul 14, 2021
My suggestion is that either Lines 160 to 161 in f9a535c ) or the underlying create_vlen_dtype should be updated, so it never puts np.str_ inside a custom vlen dtype. Instead, we should normalize element_type to always be str or bytes inside the vlen dtype. |
shoyer
commented
Jul 14, 2021
To add a bit more clarification: the vlen dtype should correspond to an HDF5/netCDF4 compatible data-type, like a variable length string or bytes. |
Something like 59ed7d5? (Obviously missing proper tests but just to get a sense of whether the idea is plausible) |
znicholls
commented
Jul 14, 2021
|
f2e1550 to
5149cc7Compareznicholls
commented
Jul 19, 2021
Ignoring failing CI due to fsspec (see #5615 (comment)) |
5149cc7 to
5e15269Compareznicholls
commented
Oct 1, 2021
@shoyer can I bother you again now that CI is passing please? |
znicholls
commented
Oct 1, 2021
@lewisjarednz fyi |
shoyer
left a comment
There was a problem hiding this comment.
Looks great, thanks! Please move the test, then we can merge this
Uh oh!
There was an error while loading. Please reload this page.
Illviljan
commented
Nov 11, 2021
Looks good to me, nice work! |
Illviljan
commented
Dec 30, 2021
Thanks @znicholls! |
Fixes handling of numpy string types in coding
pre-commit run --all-fileswhats-new.rst