From cf43256a4f10b7b208329418ccbaf1921dd23d1d Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Sun, 8 Oct 2023 18:23:13 +0200 Subject: [PATCH] Fix error when concatenating object to categorical (#1171) * Fix error when concatenating object to categorical * release note --- anndata/_core/merge.py | 2 +- anndata/tests/test_concatenate.py | 26 ++++++++++++++++++++++++++ docs/release-notes/0.10.1.md | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/anndata/_core/merge.py b/anndata/_core/merge.py index 3bb9970d5..a99eda6ac 100644 --- a/anndata/_core/merge.py +++ b/anndata/_core/merge.py @@ -264,7 +264,7 @@ def try_unifying_dtype( dtypes.add(dtype) ordered = ordered | dtype.ordered elif not pd.isnull(dtype): - return False + return None if len(dtypes) > 0 and not ordered: categories = reduce( lambda x, y: x.union(y), diff --git a/anndata/tests/test_concatenate.py b/anndata/tests/test_concatenate.py index ac9f7b0a9..318e78b3f 100644 --- a/anndata/tests/test_concatenate.py +++ b/anndata/tests/test_concatenate.py @@ -1219,6 +1219,32 @@ def test_concat_ordered_categoricals_retained(): assert c.obs["cat_ordered"].cat.ordered +def test_concat_categorical_dtype_promotion(): + """https://github.com/scverse/anndata/issues/1170 + + When concatenating categorical with other dtype, defer to pandas. + """ + a = AnnData( + np.ones((3, 3)), + obs=pd.DataFrame( + {"col": pd.Categorical(["a", "a", "b"])}, + index=[f"cell_{i:02d}" for i in range(3)], + ), + ) + b = AnnData( + np.ones((3, 3)), + obs=pd.DataFrame( + {"col": ["c", "c", "c"]}, + index=[f"cell_{i:02d}" for i in range(3, 6)], + ), + ) + + result = concat([a, b]) + expected = pd.concat([a.obs, b.obs]) + + assert_equal(result.obs, expected) + + def test_bool_promotion(): np_bool = AnnData( np.ones((5, 1)), diff --git a/docs/release-notes/0.10.1.md b/docs/release-notes/0.10.1.md index 1b83f8906..1fc710342 100644 --- a/docs/release-notes/0.10.1.md +++ b/docs/release-notes/0.10.1.md @@ -3,6 +3,8 @@ ```{rubric} Bugfix ``` +* Fix `ad.concat` erroring when concatenating a categorical and object column {pr}`1171` {user}`ivirshup` + ```{rubric} Documentation ```