Skip to content

Commit

Permalink
Fix error when concatenating object to categorical (#1171)
Browse files Browse the repository at this point in the history
* Fix error when concatenating object to categorical

* release note
  • Loading branch information
ivirshup committed Oct 8, 2023
1 parent 6a969eb commit cf43256
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion anndata/_core/merge.py
Expand Up @@ -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),
Expand Down
26 changes: 26 additions & 0 deletions anndata/tests/test_concatenate.py
Expand Up @@ -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)),
Expand Down
2 changes: 2 additions & 0 deletions docs/release-notes/0.10.1.md
Expand Up @@ -3,6 +3,8 @@
```{rubric} Bugfix
```

* Fix `ad.concat` erroring when concatenating a categorical and object column {pr}`1171` {user}`ivirshup`

```{rubric} Documentation
```

Expand Down

0 comments on commit cf43256

Please sign in to comment.