From 9ee3616e0b27bd05323c1c4337f61d107176bd37 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Wed, 23 Nov 2022 19:00:29 +0100 Subject: [PATCH] Fix order type (#856) * Fix order type * release note --- anndata/_io/specs/methods.py | 4 ++-- anndata/tests/test_io_elementwise.py | 12 ++++++++++++ docs/release-notes/0.8.1.rst | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/anndata/_io/specs/methods.py b/anndata/_io/specs/methods.py index 84bf46cec..9209a3287 100644 --- a/anndata/_io/specs/methods.py +++ b/anndata/_io/specs/methods.py @@ -636,7 +636,7 @@ def read_categorical(elem): return pd.Categorical.from_codes( codes=read_elem(elem["codes"]), categories=read_elem(elem["categories"]), - ordered=_read_attr(elem.attrs, "ordered"), + ordered=bool(_read_attr(elem.attrs, "ordered")), ) @@ -646,7 +646,7 @@ def read_categorical(elem, *, items=None, indices=(slice(None),)): return pd.Categorical.from_codes( codes=read_elem_partial(elem["codes"], indices=indices), categories=read_elem(elem["categories"]), - ordered=_read_attr(elem.attrs, "ordered"), + ordered=bool(_read_attr(elem.attrs, "ordered")), ) diff --git a/anndata/tests/test_io_elementwise.py b/anndata/tests/test_io_elementwise.py index 3a32242fb..36ed53095 100644 --- a/anndata/tests/test_io_elementwise.py +++ b/anndata/tests/test_io_elementwise.py @@ -110,3 +110,15 @@ def test_write_to_root(store): assert "anndata" == _read_attr(store.attrs, "encoding-type") assert_equal(from_disk, adata) + + +def test_categorical_order_type(store): + # https://github.com/scverse/anndata/issues/853 + cat = pd.Categorical([0, 1], ordered=True) + write_elem(store, "ordered", cat) + write_elem(store, "unordered", cat.set_ordered(False)) + + assert read_elem(store["ordered"]).ordered is True + assert type(read_elem(store["ordered"]).ordered) == bool + assert read_elem(store["unordered"]).ordered is False + assert type(read_elem(store["unordered"]).ordered) == bool diff --git a/docs/release-notes/0.8.1.rst b/docs/release-notes/0.8.1.rst index c29f4a268..910e0aef6 100644 --- a/docs/release-notes/0.8.1.rst +++ b/docs/release-notes/0.8.1.rst @@ -5,5 +5,6 @@ * Fix warning from `rename_categories` :pr:`790` :smaller:`I Virshup` * Remove backwards compat checks for categories in `uns` when we can tell the file is new enough :pr:`790` :smaller:`I Virshup` +* Categorical arrays are now created with a python `bool` instead of a `numpy.bool_` :pr:`856` .. rubric:: Documentation