anndata icon indicating copy to clipboard operation
anndata copied to clipboard

Saving categorical data triggers a warning

Open orenbenkiki opened this issue 4 years ago • 3 comments

Lately I have started seeing the following warnings whenever I write annotated data to an h5ad file:

.../site-packages/anndata/_core/anndata.py:1094: FutureWarning: is_categorical is deprecated and will be removed in a future version.  Use is_categorical_dtype instead
  if not is_categorical(df_full[k]):
.../site-packages/pandas/core/arrays/categorical.py:2487: FutureWarning: The `inplace` parameter in pandas.Categorical.remove_unused_categories is deprecated and will be removed in a future version.
  res = method(*args, **kwargs)

This is running anndata-0.7.3 with the latest pandas-1.2.1.

These warnings keep popping up from my programs, and there seems to be no easy way to disable just them. I suppose I could just disable warnings in general, but I really don't want to - in general I want to see all warnings and treat them as errors in my code wherever possible as a self-defense measure.

orenbenkiki avatar May 03 '21 15:05 orenbenkiki

I see this warning in a lot of different places with anndata 0.7.8.

I temporarily added

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

to the scirpy tutorials to reduce the noise level.

EDIT: I was too quick, the warning I receive is slightly different:

/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/anndata/_core/anndata.py:1228: FutureWarning: The `inplace` parameter in pandas.Categorical.reorder_categories is deprecated and will be removed in a future version. Reordering categories will always return a new Categorical object.
  c.reorder_categories(natsorted(c.categories), inplace=True)
... storing 'meta.structure.id' as categorical

But it might be related to the same pandas refactoring. LMK if I should open a separate issue instead.

grst avatar Nov 15 '21 13:11 grst

I would be a little more judicious with the warning filtering.

import warnings
warnings.filterwarnings("ignore", category=FutureWarning, message=r".*Reordering categories will always return a new Categorical object.*")
warnings.filterwarnings("ignore", category=FutureWarning, message=r".*is_categorical is deprecated and will be removed in a future version.*")

scottgigante-immunai avatar May 08 '22 13:05 scottgigante-immunai

Thanks, this works to hide these warnings from the end user. Should this be added to the anndata package itself?

orenbenkiki avatar May 09 '22 07:05 orenbenkiki

Closing since this has been fixed on the development branch

ivirshup avatar Dec 05 '22 10:12 ivirshup