本文整理汇总了Python中pandas.api.types.is_categorical方法的典型用法代码示例。如果您正苦于以下问题:Python types.is_categorical方法的具体用法?Python types.is_categorical怎么用?Python types.is_categorical使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.api.types
的用法示例。
在下文中一共展示了types.is_categorical方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_groups
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def get_groups(adata, groups, groupby=None):
if not isinstance(groupby, str) or groupby not in adata.obs.keys():
groupby = (
"clusters"
if "clusters" in adata.obs.keys()
else "louvain"
if "louvain" in adata.obs.keys()
else None
)
if groups is True:
return None, groupby
if groups is not None and not isinstance(groups, str) and len(groups) == 1:
groups = groups[0]
if isinstance(groups, str):
cats = [""]
if is_categorical(adata, groupby):
cats = adata.obs[groupby].cat.categories
if ":" in groups and not np.any([":" in cat for cat in cats]):
groupby, groups = groups.split(":")
groups = groups.strip()
if "," in groups and not np.any(["," in cat for cat in cats]):
groups = [a.strip() for a in groups.split(",")]
if isinstance(groups, str):
groups = [groups]
return groups, groupby
示例2: get_colors
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def get_colors(adata, c):
if is_color_like(c):
return c
else:
if f"{c}_colors" not in adata.uns.keys():
palette = default_palette(None)
palette = adjust_palette(palette, length=len(adata.obs[c].cat.categories))
n_cats = len(adata.obs[c].cat.categories)
adata.uns[f"{c}_colors"] = palette[:n_cats].by_key()["color"]
if isinstance(adata.uns[f"{c}_colors"], dict):
cluster_ix = adata.obs[c].values
else:
cluster_ix = adata.obs[c].cat.codes.values
return np.array(
[
adata.uns[f"{c}_colors"][cluster_ix[i]]
if cluster_ix[i] != -1
else "lightgrey"
for i in range(adata.n_obs)
]
)
示例3: ordinal
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def ordinal(arr):
"""
Return True if array is an ordered categorical
Parameters
----------
arr : numpy.array
Must have a dtype
Returns
-------
out : bool
Whether array `arr` is an ordered categorical
"""
if pdtypes.is_categorical(arr):
return arr.cat.ordered
return False
示例4: _remove_unused_categories
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def _remove_unused_categories(self, df_full, df_sub, uns):
from pandas.api.types import is_categorical
for k in df_full:
if not is_categorical(df_full[k]):
continue
all_categories = df_full[k].cat.categories
df_sub[k].cat.remove_unused_categories(inplace=True)
# also correct the colors...
color_key = f"{k}_colors"
if color_key not in uns:
continue
color_vec = uns[color_key]
if np.array(color_vec).ndim == 0:
# Make 0D arrays into 1D ones
uns[color_key] = np.array(color_vec)[(None,)]
elif len(color_vec) != len(all_categories):
# Reset colors
del uns[color_key]
else:
idx = np.where(np.in1d(all_categories, df_sub[k].cat.categories))[0]
uns[color_key] = np.array(color_vec)[(idx,)]
示例5: test_readwrite_zarr
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def test_readwrite_zarr(typ, tmp_path):
X = typ(X_list)
adata_src = ad.AnnData(X, obs=obs_dict, var=var_dict, uns=uns_dict)
adata_src.raw = adata_src
assert not is_categorical(adata_src.obs["oanno1"])
adata_src.write_zarr(tmp_path / "test_zarr_dir", chunks=True)
adata = ad.read_zarr(tmp_path / "test_zarr_dir")
assert is_categorical(adata.obs["oanno1"])
assert not is_categorical(adata.obs["oanno2"])
assert adata.obs.index.tolist() == ["name1", "name2", "name3"]
assert adata.obs["oanno1"].cat.categories.tolist() == ["cat1", "cat2"]
assert is_categorical(adata.raw.var["vanno2"])
assert np.all(adata.obs == adata_src.obs)
assert np.all(adata.var == adata_src.var)
assert np.all(adata.var.index == adata_src.var.index)
assert adata.var.index.dtype == adata_src.var.index.dtype
assert type(adata.raw.X) is type(adata_src.raw.X)
assert np.allclose(asarray(adata.raw.X), asarray(adata_src.raw.X))
assert np.all(adata.raw.var == adata_src.raw.var)
assert isinstance(adata.uns["uns4"]["a"], (int, np.integer))
assert isinstance(adata_src.uns["uns4"]["a"], (int, np.integer))
assert type(adata.uns["uns4"]["c"]) is type(adata_src.uns["uns4"]["c"])
assert_equal(adata, adata_src)
示例6: cat_concat
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def cat_concat(*args):
"""
Concatenate categoricals and combine the categories
Parameters
----------
*args : tuple
Categoricals to be concatenated
Examples
--------
>>> c1 = pd.Categorical(['a', 'b'], categories=['b', 'a'])
>>> c2 = pd.Categorical(['d', 'a', 'c'])
>>> cat_concat(c1, c2)
[a, b, d, a, c]
Categories (4, object): [b, a, c, d]
Notes
-----
The resulting category is not ordered.
"""
categories = pd.unique(list(chain(*(
c.categories if pdtypes.is_categorical(c) else c
for c in args
))))
cs = pd.Categorical(
list(chain(*(c for c in args))),
categories=categories
)
return cs
示例7: as_categorical
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def as_categorical(c, copy=True):
"""
Convert input to a categorical
Parameters
----------
c : categorical_like
Sequence of objects
copy : bool
If `True` and c is alread a categorical, return
a copy of `c` otherwise return `c`.
Returns
-------
out : categorical
Categorical made out of `c` or copy of `c`
if it was a categorical
"""
if not pdtypes.is_categorical(c):
c = pd.Categorical(c)
elif copy:
c = c.copy()
return c
# Temporary functions
示例8: _series_to_tensor
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def _series_to_tensor(series):
if is_categorical(series):
return torch.LongTensor(series.cat.codes.values.astype('int64'))
else: # numeric
return torch.FloatTensor(series.values)
示例9: autoprep_gbdt
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def autoprep_gbdt(algorithm_type: str, X_train: pd.DataFrame, X_test: Optional[pd.DataFrame],
categorical_feature_to_treat: Optional[List[str]] = None) -> Tuple[pd.DataFrame, pd.DataFrame]:
if categorical_feature_to_treat is None:
categorical_feature_to_treat = [c for c in X_train.columns if X_train[c].dtype.name in ['object', 'category']]
# LightGBM:
# Can handle categorical dtype. Otherwise, int, float or bool is acceptable for categorical columns.
# https://lightgbm.readthedocs.io/en/latest/Advanced-Topics.html#categorical-feature-support
#
# CatBoost:
# int, float, bool or str is acceptable for categorical columns. NaN should be filled.
# https://catboost.ai/docs/concepts/faq.html#why-float-and-nan-values-are-forbidden-for-cat-features
#
# XGBoost:
# All categorical column should be encoded beforehand.
if algorithm_type == 'lgbm':
# LightGBM can handle categorical dtype natively
categorical_feature_to_treat = [c for c in categorical_feature_to_treat if not is_categorical(X_train[c])]
if algorithm_type == 'cat' and len(categorical_feature_to_treat) > 0:
X_train = X_train.copy()
X_test = X_test.copy() if X_test is not None else X_train.iloc[:1, :].copy() # dummy
for c in categorical_feature_to_treat:
X_train[c], X_test[c] = _fill_na_by_unique_value(X_train[c], X_test[c])
if algorithm_type in ('lgbm', 'xgb') and len(categorical_feature_to_treat) > 0:
assert X_test is not None, "X_test is required for XGBoost with categorical variables"
X_train = X_train.copy()
X_test = X_test.copy()
for c in categorical_feature_to_treat:
X_train[c], X_test[c] = _fill_na_by_unique_value(X_train[c], X_test[c])
le = LabelEncoder()
concat = np.concatenate([X_train[c].values, X_test[c].values])
concat = le.fit_transform(concat)
X_train[c] = concat[:len(X_train)]
X_test[c] = concat[len(X_train):]
return X_train, X_test
示例10: _fill_na_by_unique_value
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def _fill_na_by_unique_value(strain: pd.Series, stest: Optional[pd.Series]) -> Tuple[pd.Series, pd.Series]:
if is_categorical(strain):
return strain.cat.codes, stest.cat.codes
elif is_integer_dtype(strain.dtype):
fillval = min(strain.min(), stest.min()) - 1
return strain.fillna(fillval), stest.fillna(fillval)
else:
return strain.astype(str), stest.astype(str)
示例11: is_categorical
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def is_categorical(data, c=None):
from pandas.api.types import is_categorical as cat
if c is None:
return cat(data) # if data is categorical/array
if not is_view(data): # if data is anndata view
strings_to_categoricals(data)
return isinstance(c, str) and c in data.obs.keys() and cat(data.obs[c])
示例12: get_value_counts
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def get_value_counts(adata, color):
value_counts = adata.obs[color].value_counts()
probs = np.array(adata.obs[color])
for cat in value_counts.index:
probs[probs == cat] = value_counts[cat]
return np.array(probs, dtype=np.float32)
示例13: default_legend_loc
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def default_legend_loc(adata, color, legend_loc):
n_categories = 0
if is_categorical(adata, color):
n_categories = len(adata.obs[color].cat.categories)
if legend_loc is False:
legend_loc = "none"
elif legend_loc is None:
legend_loc = "upper right" if n_categories <= 4 else "on data"
return legend_loc
示例14: compute_group
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def compute_group(cls, data, scales, **params):
labels = ['x', 'y']
X = np.array(data[labels])
res = boxplot_stats(X, whis=params['coef'], labels=labels)[1]
try:
n = data['weight'].sum()
except KeyError:
n = len(data['y'])
if len(np.unique(data['x'])) > 1:
width = np.ptp(data['x']) * 0.9
else:
width = params['width']
if pdtypes.is_categorical(data['x']):
x = data['x'].iloc[0]
else:
x = np.mean([data['x'].min(), data['x'].max()])
d = {'ymin': res['whislo'],
'lower': res['q1'],
'middle': [res['med']],
'upper': res['q3'],
'ymax': res['whishi'],
'outliers': [res['fliers']],
'notchupper': res['med']+1.58*res['iqr']/np.sqrt(n),
'notchlower': res['med']-1.58*res['iqr']/np.sqrt(n),
'x': x,
'width': width,
'relvarwidth': np.sqrt(n)}
return pd.DataFrame(d)
示例15: test_readwrite_h5ad
# 需要导入模块: from pandas.api import types [as 别名]
# 或者: from pandas.api.types import is_categorical [as 别名]
def test_readwrite_h5ad(typ, dataset_kwargs, backing_h5ad):
tmpdir = tempfile.TemporaryDirectory()
tmpdirpth = Path(tmpdir.name)
mid_pth = tmpdirpth / "mid.h5ad"
X = typ(X_list)
adata_src = ad.AnnData(X, obs=obs_dict, var=var_dict, uns=uns_dict)
assert not is_categorical(adata_src.obs["oanno1"])
adata_src.raw = adata_src
adata_src.write(backing_h5ad, **dataset_kwargs)
adata_mid = ad.read(backing_h5ad)
adata_mid.write(mid_pth, **dataset_kwargs)
adata = ad.read_h5ad(mid_pth)
assert is_categorical(adata.obs["oanno1"])
assert not is_categorical(adata.obs["oanno2"])
assert adata.obs.index.tolist() == ["name1", "name2", "name3"]
assert adata.obs["oanno1"].cat.categories.tolist() == ["cat1", "cat2"]
assert is_categorical(adata.raw.var["vanno2"])
assert np.all(adata.obs == adata_src.obs)
assert np.all(adata.var == adata_src.var)
assert np.all(adata.var.index == adata_src.var.index)
assert adata.var.index.dtype == adata_src.var.index.dtype
assert type(adata.raw.X) is type(adata_src.raw.X)
assert type(adata.raw.varm) is type(adata_src.raw.varm)
assert np.allclose(asarray(adata.raw.X), asarray(adata_src.raw.X))
assert np.all(adata.raw.var == adata_src.raw.var)
assert isinstance(adata.uns["uns4"]["a"], (int, np.integer))
assert isinstance(adata_src.uns["uns4"]["a"], (int, np.integer))
assert type(adata.uns["uns4"]["c"]) is type(adata_src.uns["uns4"]["c"])
assert_equal(adata, adata_src)