Boolean confusion
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
True and True are treated different depending on whether it is a bool or a np.bool_.
This creates absolute hellish errors. You save date, and load it again. Then bool turned to np.bool_ and things work differently.
I.e. you cannot save your data again.
The following example works perfectly fine:
>>> data = xr.Dataset()
>>> data.attrs['bool_type'] = True
>>> data.to_netcdf()
>>> data
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
bool_type: True
The following example however fails:
>>> data = xr.Dataset()
>>> data.attrs['bool_type'] = np.True_
>>> data.to_netcdf()
TypeError: Invalid value for attr: True must be a number, a string, an ndarray or a list/tuple of numbers/strings for serialization to netCDF files
>>> data
<xarray.Dataset>
Dimensions: ()
Data variables:
*empty*
Attributes:
bool_type: True
If you load the data again, in both cases np.True_ leading to confusing things, when the seemingly same things fails.
For this routine a simple remedy is probably expanding the instance check in check_attr:
isinstance(value, (str, Number, np.ndarray, np.number, np.bool_, list, tuple))
As numpy warns, np.bool_ are no numbers in contrast to regular Python, see https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.bool_.
I fear, however, that this problem runs deeper and the same issue might arise at different points.
edit: included TypeError
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two Dataset.attr examples with data.to_netcdf(), then inspect the check_attr validation mentioned in the issue. Verify that np.bool_ attributes serialize successfully and that the save-and-reload behavior is consistent with regular Python booleans.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100