Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for numpy.bool_ #4986

Merged
merged 7 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ def check_attr(name, value):
"serialization to netCDF files"
)

if not isinstance(value, (str, Number, np.ndarray, np.number, list, tuple)):
if not isinstance(
value, (str, Number, np.ndarray, np.number, np.bool_, list, tuple)
caenrigen marked this conversation as resolved.
Show resolved Hide resolved
):
raise TypeError(
f"Invalid value for attr {name!r}: {value!r} must be a number, "
"a string, an ndarray or a list/tuple of "
"a string, an ndarray, a bool/numpy.bool_, or a list/tuple of "
"numbers/strings for serialization to netCDF "
"files"
)
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,14 @@ def test_complex(self, invalid_netcdf, warntype, num_warns):

assert recorded_num_warns == num_warns

def test_numpy_bool_(self):
# h5netcdf loads booleans as numpy.bool_, this type needs to be supported
# when writing invalid_netcdf datasets in order to support a roundtrip
expected = Dataset({"x": ("y", np.ones(5), {"numpy_bool": np.bool_(True)})})
save_kwargs = {"invalid_netcdf": True}
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_equal(expected, actual)
caenrigen marked this conversation as resolved.
Show resolved Hide resolved

def test_cross_engine_read_write_netcdf4(self):
# Drop dim3, because its labels include strings. These appear to be
# not properly read with python-netCDF4, which converts them into
Expand Down