Support DataArrays with names that are not string
malmans2 opened this issue · 4 comments
Right now pretty much everything is broken if any DataArray
in a Dataset
has a name that is not a string.
However, I don't think we have to introduce too many changes to fix this, it involves something very similar to what we did in #227.
What do you think, does it makes sense to support these kind of Datasets
?
👍, technically variable names can be Hashable
, they're typed like that. So we can relax the str
typing slowly. I initially had it typed as Hashable
but then the errors were too hard to fix :/ so I switched to str in the mean time.
I think we can do this incrementally by changing the typing pretty low down in apply_mapper
and rewrite_values
and work up from there.
Moving my other issue to here, because it's the same base problem.
The use of ds._to_temp_dataset()
is broken with cf_xarray
because the resulting data variable doesn't have string name, but rather some xarray ReprObject.
import xarray as xr
import cf_xarray as cfxr
ds = xr.Dataset(
{'a': (('x',), [1, 2, 3, 4])},
coords={ 'x': [2, 3, 4, 5]}
)
print(cfxr.__version__)
print(ds.a._to_temp_dataset().cf)
Fails with:
Traceback
File "/home/aulemahal/test.py", line 14, in <module>
print(ds.a._to_temp_dataset().cf)
File "/home/aulemahal/.pyenvs/testcfxr/lib/python3.9/site-packages/cf_xarray/accessor.py", line 1283, in __repr__
text += make_text_section("Bounds", "bounds", coords)
File "/home/aulemahal/.pyenvs/testcfxr/lib/python3.9/site-packages/cf_xarray/accessor.py", line 1229, in make_text_section
vardict = getattr(self, attr, {})
File "/home/aulemahal/.pyenvs/testcfxr/lib/python3.9/site-packages/cf_xarray/accessor.py", line 1910, in bounds
vardict = {
File "/home/aulemahal/.pyenvs/testcfxr/lib/python3.9/site-packages/cf_xarray/accessor.py", line 1912, in <dictcomp>
apply_mapper(_get_bounds, obj, key, error=False)
File "/home/aulemahal/.pyenvs/testcfxr/lib/python3.9/site-packages/cf_xarray/accessor.py", line 93, in apply_mapper
raise ValueError("`default` must be provided when `key` is not a string.")
ValueError: `default` must be provided when `key` is not a string.
Because key
is an instance of xr.core.utils.ReprObject
.
Yes lets fix this. A general solution would be nice, but using isinstance(ReprObject)
would be an OK quick fix. It must be the most common non-string name.