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

cast numpy scalars to arrays in as_compatible_data #9403

Merged
merged 11 commits into from
Sep 23, 2024
6 changes: 4 additions & 2 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ def convert_non_numpy_type(data):
else:
data = np.asarray(data)

if not isinstance(data, np.ndarray) and (
# immediately return array-like types except `numpy.ndarray` subclasses and `numpy` scalars
if not isinstance(data, np.ndarray | np.generic) and (
Copy link
Member

Choose a reason for hiding this comment

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

This is worth adding a comment, noting that we want to cast numpy scalars to arrays.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the latest commit does that, but please check if that's clear enough

hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")
):
return cast("T_DuckArray", data)

# validate whether the data is valid data types.
# validate whether the data is valid data types. Also, explicitly cast `numpy`
# subclasses and `numpy` scalars to `numpy.ndarray`
data = np.asarray(data)

if data.dtype.kind in "OMm":
Expand Down
7 changes: 6 additions & 1 deletion xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,12 @@ def test_unchanged_types(self):
assert source_ndarray(x) is source_ndarray(as_compatible_data(x))

def test_converted_types(self):
for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]:
for input_array in [
[[0, 1, 2]],
pd.DataFrame([[0, 1, 2]]),
np.float64(1.4),
np.str_("abc"),
]:
actual = as_compatible_data(input_array)
assert_array_equal(np.asarray(input_array), actual)
assert np.ndarray is type(actual)
Expand Down
Loading