Skip to content

Commit

Permalink
Fix order type (#856)
Browse files Browse the repository at this point in the history
* Fix order type

* release note
ivirshup authored Nov 23, 2022
1 parent 0dc3cf9 commit 9ee3616
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
@@ -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")),
)


12 changes: 12 additions & 0 deletions anndata/tests/test_io_elementwise.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions docs/release-notes/0.8.1.rst
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9ee3616

Please sign in to comment.