Skip to content

Commit f91306a

Browse files
keewiskmuehlbauer
andauthored
cast numpy scalars to arrays in NamedArray.from_array (#10008)
* check that aggregations result in array objects * don't consider numpy scalars as arrays * changelog [skip-ci] * retrigger CI * Update xarray/tests/test_namedarray.py --------- Co-authored-by: Kai Mühlbauer <kmuehlbauer@wradlib.org>
1 parent e84e421 commit f91306a

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Bug fixes
106106
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
107107
- Fix weighted ``polyfit`` for arrays with more than two dimensions (:issue:`9972`, :pull:`9974`).
108108
By `Mattia Almansi <https://github.com/malmans2>`_.
109+
- Cast ``numpy`` scalars to arrays in :py:meth:`NamedArray.from_arrays` (:issue:`10005`, :pull:`10008`)
110+
By `Justus Magin <https://github.com/keewis>`_.
109111

110112
Documentation
111113
~~~~~~~~~~~~~

xarray/namedarray/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def from_array(
205205

206206
return NamedArray(dims, data, attrs)
207207

208-
if isinstance(data, _arrayfunction_or_api):
208+
if isinstance(data, _arrayfunction_or_api) and not isinstance(data, np.generic):
209209
return NamedArray(dims, data, attrs)
210210

211211
if isinstance(data, tuple):

xarray/tests/test_namedarray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ def test_warn_on_repeated_dimension_names(self) -> None:
591591
with pytest.warns(UserWarning, match="Duplicate dimension names"):
592592
NamedArray(("x", "x"), np.arange(4).reshape(2, 2))
593593

594+
def test_aggregation(self) -> None:
595+
x: NamedArray[Any, np.dtype[np.int64]]
596+
x = NamedArray(("x", "y"), np.arange(4).reshape(2, 2))
597+
598+
result = x.sum()
599+
assert isinstance(result.data, np.ndarray)
600+
594601

595602
def test_repr() -> None:
596603
x: NamedArray[Any, np.dtype[np.uint64]]

0 commit comments

Comments
 (0)