Skip to content

Commit

Permalink
Fix error when concatenating object to categorical
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirshup committed Oct 8, 2023
1 parent 6a969eb commit 433190d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion anndata/_core/merge.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 433190d

Please sign in to comment.