pydata / pydata/xarray

Boolean confusion

Open
#5,254 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic-backends
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.