Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,16 @@ def convert_non_numpy_type(data):
else:
data = np.asarray(data)

if not isinstance(data, np.ndarray) and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
if (
not isinstance(data, np.ndarray)
and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
)
# Not exactly sure why this is a special case but
# https://github.com/pydata/xarray/issues/9535
# (possibly numpy strings can be
# indexed but it's indexing the string rather than as an array?)
and not isinstance(data, np.str_)
Comment on lines +324 to +332
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as pointed out in #9535 I believe this will be resolved by #9403, but in case it doesn't, can we combine the isinstance checks?

Suggested change
not isinstance(data, np.ndarray)
and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
)
# Not exactly sure why this is a special case but
# https://github.com/pydata/xarray/issues/9535
# (possibly numpy strings can be
# indexed but it's indexing the string rather than as an array?)
and not isinstance(data, np.str_)
# Not exactly sure why this is a special case but
# https://github.com/pydata/xarray/issues/9535
# (possibly numpy strings can be
# indexed but it's indexing the string rather than as an array?)
not isinstance(data, np.ndarray | np.str_)
and (
hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
)

):
return cast("T_DuckArray", data)

Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3068,3 +3068,12 @@ def test_pandas_indexing_adapter_non_nanosecond_conversion(index, dtype) -> None
with pytest.warns(UserWarning, match="non-nanosecond precision"):
var = Variable(["time"], data)
assert var.dtype == np.dtype(f"{dtype}[ns]")


def test_numpy_strings():
# https://github.com/pydata/xarray/issues/9535
data = np.full((5,), fill_value="nothing")
da = DataArray(data)
values = np.asarray(["foo", "bar"])
da[1:2] = values[0]
assert da.sel(dim_0=1).item() == "foo"
Loading