Skip to content

Commit cb3ad04

Browse files
Prevent pydap (dap4) to change string arrays to unicode type (testing). Fixed upstream (#10482)
Co-authored-by: Kai Mühlbauer <kai.muehlbauer@uni-bonn.de>
1 parent 516ec07 commit cb3ad04

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

ci/requirements/min-all-deps.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies:
3838
- pandas=2.2
3939
- pint=0.24
4040
- pip
41-
- pydap=3.5
41+
- pydap=3.5.0
4242
- pytest
4343
- pytest-cov
4444
- pytest-env

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Bug fixes
117117
are passed (:pull:`10440`). By `Mathias Hauser <https://github.com/mathause>`_.
118118
- Fix :py:func:`testing.assert_equal` with ``check_dim_order=False`` for :py:class:`DataTree` objects
119119
(:pull:`10442`). By `Mathias Hauser <https://github.com/mathause>`_.
120+
- Fix Pydap backend testing. Now test forces string arrays to dtype "S" (pydap converts them to unicode type by default). Removes conditional to numpy version. (:issue:`10261`, :pull:`10482`)
121+
By `Miguel Jimenez-Urias <https://github.com/Mikejmnez>`_.
120122

121123

122124
Documentation

xarray/tests/test_backends.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,7 +5437,9 @@ def convert_to_pydap_dataset(self, original):
54375437

54385438
ds = DatasetType("bears", **original.attrs)
54395439
for key, var in original.data_vars.items():
5440-
ds[key] = BaseType(key, var.values, dims=var.dims, **var.attrs)
5440+
ds[key] = BaseType(
5441+
key, var.values, dtype=var.values.dtype.kind, dims=var.dims, **var.attrs
5442+
)
54415443
# check all dims are stored in ds
54425444
for d in original.coords:
54435445
ds[d] = BaseType(d, original[d].values, dims=(d,), **original[d].attrs)
@@ -5449,9 +5451,9 @@ def create_datasets(self, **kwargs):
54495451
# print("QQ0:", expected["bears"].load())
54505452
pydap_ds = self.convert_to_pydap_dataset(expected)
54515453
actual = open_dataset(PydapDataStore(pydap_ds))
5452-
if Version(np.__version__) < Version("2.3.0"):
5453-
# netcdf converts string to byte not unicode
5454-
expected["bears"] = expected["bears"].astype(str)
5454+
# netcdf converts string to byte not unicode
5455+
# fixed in pydap 3.5.6. https://github.com/pydap/pydap/issues/510
5456+
actual["bears"].values = actual["bears"].values.astype("S")
54555457
yield actual, expected
54565458

54575459
def test_cmp_local_file(self) -> None:
@@ -5495,9 +5497,6 @@ def test_compatible_to_netcdf(self) -> None:
54955497
with create_tmp_file() as tmp_file:
54965498
actual.to_netcdf(tmp_file)
54975499
with open_dataset(tmp_file) as actual2:
5498-
if Version(np.__version__) < Version("2.3.0"):
5499-
# netcdf converts string to byte not unicode
5500-
actual2["bears"] = actual2["bears"].astype(str)
55015500
assert_equal(actual2, expected)
55025501

55035502
@requires_dask
@@ -5515,9 +5514,11 @@ def create_dap2_datasets(self, **kwargs):
55155514
# in pydap 3.5.0, urls defaults to dap2.
55165515
url = "http://test.opendap.org/opendap/data/nc/bears.nc"
55175516
actual = open_dataset(url, engine="pydap", **kwargs)
5517+
# pydap <3.5.6 converts to unicode dtype=|U. Not what
5518+
# xarray expects. Thus force to bytes dtype. pydap >=3.5.6
5519+
# does not convert to unicode. https://github.com/pydap/pydap/issues/510
5520+
actual["bears"].values = actual["bears"].values.astype("S")
55185521
with open_example_dataset("bears.nc") as expected:
5519-
# workaround to restore string which is converted to byte
5520-
expected["bears"] = expected["bears"].astype(str)
55215522
yield actual, expected
55225523

55235524
def output_grid_deprecation_warning_dap2dataset(self):
@@ -5530,7 +5531,8 @@ def create_dap4_dataset(self, **kwargs):
55305531
actual = open_dataset(url, engine="pydap", **kwargs)
55315532
with open_example_dataset("bears.nc") as expected:
55325533
# workaround to restore string which is converted to byte
5533-
expected["bears"] = expected["bears"].astype(str)
5534+
# only needed for pydap <3.5.6 https://github.com/pydap/pydap/issues/510
5535+
expected["bears"].values = expected["bears"].values.astype("S")
55345536
yield actual, expected
55355537

55365538
def test_session(self) -> None:

0 commit comments

Comments
 (0)