Skip to content

Commit 4f5b293

Browse files
authored
Skip one more hypothesis test for dtype string (#11152)
Includes a few small fixes: * When the key is `dim` using **kwargs doesn't yield the same result as using the positional argument. Consider making this a positional argument only field in the future. * Coerce pd.Index to object if string dtype fails
1 parent faeae19 commit 4f5b293

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

properties/test_index_manipulation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ def assert_invariants(self):
269269
DatasetTest = DatasetStateMachine.TestCase
270270

271271

272+
@pytest.mark.skip(reason="failure detected by hypothesis")
273+
def test_unstack_string():
274+
ds = xr.Dataset()
275+
ds["0"] = np.array(["", "0", "\x000"], dtype="<U2")
276+
ds.stack({"1": ["0"]}).unstack()
277+
278+
272279
@pytest.mark.skip(reason="failure detected by hypothesis")
273280
def test_unstack_object():
274281
ds = xr.Dataset()

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5205,7 +5205,7 @@ def _stack_once(
52055205
vdims = list(var.dims) + add_dims
52065206
shape = [self.sizes[d] for d in vdims]
52075207
exp_var = var.set_dims(vdims, shape)
5208-
stacked_var = exp_var.stack(**{new_dim: dims})
5208+
stacked_var = exp_var.stack({new_dim: dims})
52095209
new_variables[name] = stacked_var
52105210
stacked_var_names.append(name)
52115211
else:

xarray/core/indexes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,13 @@ def safe_cast_to_index(array: Any) -> pd.Index:
536536
)
537537
kwargs["dtype"] = "float64"
538538

539-
index = pd.Index(to_numpy(array), **kwargs)
539+
values = to_numpy(array)
540+
try:
541+
index = pd.Index(values, **kwargs)
542+
except UnicodeEncodeError:
543+
# coerce to object if pandas fails to coerce to string
544+
kwargs["dtype"] = "object"
545+
index = pd.Index(values, **kwargs)
540546

541547
return _maybe_cast_to_cftimeindex(index)
542548

0 commit comments

Comments
 (0)