def strings_to_categoricals(adata):
from pandas import Categorical<EOL>def is_valid_dtype(values):<EOL><INDENT>from pandas.api.types import is_string_dtype, is_integer_dtype, is_bool_dtype<EOL>return is_string_dtype(values) or is_integer_dtype(values) or is_bool_dtype(values)<EOL><DEDENT>for df in [adata.obs, adata.var]:<EOL><INDENT>string_cols = [key for key in df.columns if is_valid_dtype(df[key])]<EOL>for key in string_cols:<EOL><INDENT>c = df[key]<EOL>c = Categorical(c)<EOL>if len(c.categories) < min(len(c), <NUM_LIT:100>): df[key] = c<EOL><DEDENT><DEDENT>
Transform string annotations to categoricals.