Skip to content

ad.concat type unification fails for object + categorical in AnnData >= 0.10 #1170

Closed

Description

@jeskowagner

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of anndata.
  • (optional) I have confirmed this bug exists on the master branch of anndata.

Report

AnnData's 0.10 update changes dtype promotion of concat, which may represent an undesired regression (or a desired sanity check). Concatenating object and category would automatically be promoted to object up until 0.9.2, and fails with below traceback from 0.10. This is also true for the current anndata-0.11.0.dev0 and was presumably introduced in #992. This could be desired behavior, although a gentle error message could provide a better pointer on how to fix the issue.

Code:

import anndata as ad
import pandas as pd
import numpy as np

one = ad.AnnData(X=np.array([[1]]))
one.obs = pd.DataFrame(["a"], index=['0'], columns=["col"])

two = ad.AnnData(X=np.array([[2]]))
two.obs = pd.DataFrame(["b"], index=['0'], columns=["col"])
two.obs["col"] = two.obs["col"].astype("category")

ad.concat([one,two])

Traceback:

                                                                                                            
File ".../lib/python3.11/site-packages/anndata/_core/merge.py", line 1255, in concat                                                                                        
  unify_dtypes([getattr(a, dim) for a in adatas]),
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.11/site-packages/anndata/_core/merge.py", line 238, in unify_dtypes
  df[col] = df[col].astype(dtype)
            ^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.11/site-packages/pandas/core/generic.py", line 6534, in astype
  new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.11/site-packages/pandas/core/internals/managers.py", line 414, in astype
  return self.apply(
          ^^^^^^^^^^^
File ".../lib/python3.11/site-packages/pandas/core/internals/managers.py", line 354, in apply
  applied = getattr(b, f)(**kwargs) 
            ^^^^^^^^^^^^^^^^^^^^^^^ 
File ".../lib/python3.11/site-packages/pandas/core/internals/blocks.py", line 616, in astype
  new_values = astype_array_safe(values, dtype, copy=copy, errors=errors) 
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
File ".../lib/python3.11/site-packages/pandas/core/dtypes/astype.py", line 232, in astype_array_safe
  dtype = pandas_dtype(dtype)
          ^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.11/site-packages/pandas/core/dtypes/common.py", line 1636, in pandas_dtype
  npdtype = np.dtype(dtype)
            ^^^^^^^^^^^^^^^
TypeError: Cannot interpret 'False' as a data type

Versions

anndata             0.10.0
session_info        1.0.0

cython_runtime      NA
dateutil            2.8.2
h5py                3.9.0
natsort             8.4.0
numpy               1.24.4
packaging           23.2
pandas              2.1.1
pytz                2023.3.post1
scipy               1.11.3
six                 1.16.0

Python 3.11.6 | packaged by conda-forge | (main, Oct  3 2023, 10:40:35) [GCC 12.3.0]
Linux-<redacted>-x86_64-x86_64

Session information updated at 2023-10-08 11:32

Activity

  1. LucaMarconato commented on Oct 8, 2023

    @LucaMarconato
    Member
  2. ivirshup commented on Oct 8, 2023

    @ivirshup
    Member

    Thanks for the report! The issue was more of a typo, we were returning False from try_unifying_dtype when we should be returning None. PR to fix: 1171

  3. ivirshup commented on Oct 8, 2023

    @ivirshup
    Member

    It could be nice for concatenation to automatically upcast appropriate types to categorical, but that's not the scope of the original feature so I've just fixed this error.

    If you ran into this error via a test suite you have, it'd be great if you could add a job to your CI testing against pre-release candidates of upstream packages. That way we can find and fix these errors before they go out into a release.

    Here's an example of a github actions workflow from the scverse cookie cutter template that does this:

  4. ivirshup commented on Oct 8, 2023

    @ivirshup
    Member

    Bug fix release made

  5. LucaMarconato commented on Oct 8, 2023

    @LucaMarconato
    Member

    It fixed the issue, thanks! I will add the test against pre-release packages.

  6. added theissue type on Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions