From 2b0a075c1ae8b11f636eb5e814aec57eeedcc601 Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Mon, 19 Dec 2022 23:27:35 -0500 Subject: [PATCH 1/3] return multiindex levels as original dtype (#7250) --- doc/whats-new.rst | 1 + xarray/core/indexing.py | 6 +++++- xarray/tests/test_indexes.py | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index cd5ecd83978..65b9977fc61 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -45,6 +45,7 @@ Bug fixes - add a ``keep_attrs`` parameter to :py:meth:`Dataset.pad`, :py:meth:`DataArray.pad`, and :py:meth:`Variable.pad` (:pull:`7267`). By `Justus Magin `_. +- Preserve original `dtype` on accessing MultiIndex levels (:issue:`7250`) Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index ba937183d24..e49a072df1d 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -1531,8 +1531,12 @@ def __init__( self.level = level def __array__(self, dtype: DTypeLike = None) -> np.ndarray: + if dtype is None: + dtype = self.dtype if self.level is not None: - return self.array.get_level_values(self.level).values + return np.asarray( + self.array.get_level_values(self.level).values, dtype=dtype + ) else: return super().__array__(dtype) diff --git a/xarray/tests/test_indexes.py b/xarray/tests/test_indexes.py index 3ecfa73cc89..c53f3508962 100644 --- a/xarray/tests/test_indexes.py +++ b/xarray/tests/test_indexes.py @@ -697,3 +697,10 @@ def test_safe_cast_to_index_datetime_datetime(): actual = safe_cast_to_index(np.array(dates)) assert_array_equal(expected, actual) assert isinstance(actual, pd.Index) + + +@pytest.mark.parametrize("dtype", ["int32", "float32"]) +def test_restore_dtype_on_multiindexes(dtype): + foo = xr.Dataset(coords={"bar": ("bar", np.array([0, 1], dtype=dtype))}) + foo = foo.stack(baz=("bar",)) + assert str(foo["bar"].values.dtype) == dtype From 08d5b59aa233bd9aa9754e796ee0b7cab362d3d0 Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Tue, 20 Dec 2022 08:57:39 -0500 Subject: [PATCH 2/3] typing in test, contributor attribution --- doc/whats-new.rst | 3 ++- xarray/tests/test_indexes.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 65b9977fc61..39a42f089fb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -45,7 +45,8 @@ Bug fixes - add a ``keep_attrs`` parameter to :py:meth:`Dataset.pad`, :py:meth:`DataArray.pad`, and :py:meth:`Variable.pad` (:pull:`7267`). By `Justus Magin `_. -- Preserve original `dtype` on accessing MultiIndex levels (:issue:`7250`) +- Preserve original `dtype` on accessing MultiIndex levels (:issue:`7250`, + :pull:`7393`). By `Ian Carroll `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/tests/test_indexes.py b/xarray/tests/test_indexes.py index c53f3508962..caf0f51135e 100644 --- a/xarray/tests/test_indexes.py +++ b/xarray/tests/test_indexes.py @@ -700,7 +700,7 @@ def test_safe_cast_to_index_datetime_datetime(): @pytest.mark.parametrize("dtype", ["int32", "float32"]) -def test_restore_dtype_on_multiindexes(dtype): +def test_restore_dtype_on_multiindexes(dtype: str) -> None: foo = xr.Dataset(coords={"bar": ("bar", np.array([0, 1], dtype=dtype))}) foo = foo.stack(baz=("bar",)) assert str(foo["bar"].values.dtype) == dtype From c3cdc5823ef1765bac37106a38b5f1c54370167f Mon Sep 17 00:00:00 2001 From: Ian Carroll Date: Tue, 20 Dec 2022 09:41:13 -0500 Subject: [PATCH 3/3] rst is not markdown --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 39a42f089fb..0b7fc87201b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -45,7 +45,7 @@ Bug fixes - add a ``keep_attrs`` parameter to :py:meth:`Dataset.pad`, :py:meth:`DataArray.pad`, and :py:meth:`Variable.pad` (:pull:`7267`). By `Justus Magin `_. -- Preserve original `dtype` on accessing MultiIndex levels (:issue:`7250`, +- Preserve original dtype on accessing MultiIndex levels (:issue:`7250`, :pull:`7393`). By `Ian Carroll `_. Documentation