Module « pandas »
Classe « Categorical »
Informations générales
Héritage
builtins.object
ABC
BaseStringArrayMethods
ObjectStringArrayMixin
builtins.object
DirNamesMixin
PandasObject
builtins.object
ExtensionArray
NDArrayBackedExtensionArray
Categorical
Définition
class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin):
Description [extrait de Categorical.__doc__]
Represent a categorical variable in classic R / S-plus fashion.
`Categoricals` can only take on only a limited, and usually fixed, number
of possible values (`categories`). In contrast to statistical categorical
variables, a `Categorical` might have an order, but numerical operations
(additions, divisions, ...) are not possible.
All values of the `Categorical` are either in `categories` or `np.nan`.
Assigning values outside of `categories` will raise a `ValueError`. Order
is defined by the order of the `categories`, not lexical order of the
values.
Parameters
----------
values : list-like
The values of the categorical. If categories are given, values not in
categories will be replaced with NaN.
categories : Index-like (unique), optional
The unique categories for this categorical. If not given, the
categories are assumed to be the unique values of `values` (sorted, if
possible, otherwise in the order in which they appear).
ordered : bool, default False
Whether or not this categorical is treated as a ordered categorical.
If True, the resulting categorical will be ordered.
An ordered categorical respects, when sorted, the order of its
`categories` attribute (which in turn is the `categories` argument, if
provided).
dtype : CategoricalDtype
An instance of ``CategoricalDtype`` to use for this categorical.
Attributes
----------
categories : Index
The categories of this categorical
codes : ndarray
The codes (integer positions, which point to the categories) of this
categorical, read only.
ordered : bool
Whether or not this Categorical is ordered.
dtype : CategoricalDtype
The instance of ``CategoricalDtype`` storing the ``categories``
and ``ordered``.
Methods
-------
from_codes
__array__
Raises
------
ValueError
If the categories do not validate.
TypeError
If an explicit ``ordered=True`` is given but no `categories` and the
`values` are not sortable.
See Also
--------
CategoricalDtype : Type for categorical data.
CategoricalIndex : An Index with an underlying ``Categorical``.
Notes
-----
See the `user guide
<https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html>`_
for more.
Examples
--------
>>> pd.Categorical([1, 2, 3, 1, 2, 3])
[1, 2, 3, 1, 2, 3]
Categories (3, int64): [1, 2, 3]
>>> pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
['a', 'b', 'c', 'a', 'b', 'c']
Categories (3, object): ['a', 'b', 'c']
Missing values are not included as a category.
>>> c = pd.Categorical([1, 2, 3, 1, 2, 3, np.nan])
>>> c
[1, 2, 3, 1, 2, 3, NaN]
Categories (3, int64): [1, 2, 3]
However, their presence is indicated in the `codes` attribute
by code `-1`.
>>> c.codes
array([ 0, 1, 2, 0, 1, 2, -1], dtype=int8)
Ordered `Categoricals` can be sorted according to the custom order
of the categories and can have a min and max value.
>>> c = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'], ordered=True,
... categories=['c', 'b', 'a'])
>>> c
['a', 'b', 'c', 'a', 'b', 'c']
Categories (3, object): ['c' < 'b' < 'a']
>>> c.min()
'c'
Constructeur(s)
Liste des attributs statiques
itemsize | <pandas._libs.properties.CachedProperty object at 0x7f504c4d4880> |
ndim | <pandas._libs.properties.CachedProperty object at 0x7f504c4ffbc0> |
size | <pandas._libs.properties.CachedProperty object at 0x7f504c4ffec0> |
Attributs statiques hérités de la classe NDArrayBackedExtensionArray
nbytes
Liste des propriétés
categories | |
codes | |
dtype | |
nbytes | |
ordered | |
shape | |
T | |
Propriétés héritées de la classe ExtensionArray
ndim, size
Opérateurs hérités de la classe NDArrayBackedExtensionArray
__setitem__
Opérateurs hérités de la classe ExtensionArray
__eq__, __ne__
Opérateurs hérités de la classe object
__ge__,
__gt__,
__le__,
__lt__
Liste des méthodes
Toutes les méthodes
Méthodes d'instance
Méthodes statiques
Méthodes dépréciées
__array__(self, dtype=None) -> numpy.ndarray |
|
__array_ufunc__(self, ufunc, method, *inputs, **kwargs) |
|
__iter__(self) |
|
__repr__(self) -> str |
|
__setstate__(self, state) |
Necessary for making this object picklable [extrait de __setstate__.__doc__] |
add_categories(self, new_categories, inplace=False) |
|
argsort(self, ascending=True, kind='quicksort', **kwargs) |
|
as_ordered(self, inplace=False) |
|
as_unordered(self, inplace=False) |
|
astype(self, dtype: Union[ForwardRef('ExtensionDtype'), str, numpy.dtype, Type[Union[str, float, int, complex, bool, object]]], copy: bool = True) -> ~ArrayLike |
|
check_for_ordered(self, op) |
assert that we are ordered [extrait de check_for_ordered.__doc__] |
describe(self) |
|
equals(self, other: object) -> bool |
|
fillna(self, value=None, method=None, limit=None) |
|
from_codes(codes, categories=None, ordered=None, dtype=None) |
|
is_dtype_equal(self, other) -> bool |
|
isin(self, values) -> numpy.ndarray |
|
isna(self) |
|
isnull(self) |
|
map(self, mapper) |
|
max(self, *, skipna=True, **kwargs) |
|
memory_usage(self, deep: bool = False) -> int |
|
min(self, *, skipna=True, **kwargs) |
|
mode(self, dropna=True) |
|
notna(self) |
|
notnull(self) |
|
remove_categories(self, removals, inplace=False) |
|
remove_unused_categories(self, inplace=<object object at 0x7f5051439e10>) |
|
rename_categories(self, new_categories, inplace=False) |
|
reorder_categories(self, new_categories, ordered=None, inplace=False) |
|
replace(self, to_replace, value, inplace: bool = False) |
|
set_categories(self, new_categories, ordered=None, rename=False, inplace=False) |
|
set_ordered(self, value, inplace=False) |
|
sort_values(self, inplace: bool = False, ascending: bool = True, na_position: str = 'last') |
|
take_nd(self, indexer, allow_fill: bool = False, fill_value=None) |
|
to_dense(self) |
|
to_list(self) -> List[Union[str, int, float, bool, ForwardRef('Period'), ForwardRef('Timestamp'), ForwardRef('Timedelta'), ForwardRef('Interval')]] |
|
tolist(self) -> List[Union[str, int, float, bool, ForwardRef('Period'), ForwardRef('Timestamp'), ForwardRef('Timedelta'), ForwardRef('Interval')]] |
|
unique(self) |
|
value_counts(self, dropna=True) |
|
view(self, dtype=None) |
|
Méthodes héritées de la classe ObjectStringArrayMixin
__init_subclass__, __len__, __subclasshook__
Méthodes héritées de la classe PandasObject
__sizeof__
Méthodes héritées de la classe DirNamesMixin
__dir__
Méthodes héritées de la classe NDArrayBackedExtensionArray
copy, putmask, ravel, repeat, reshape, searchsorted, shift, take, where
Méthodes héritées de la classe ExtensionArray
__hash__, argmax, argmin, dropna, factorize, to_numpy, transpose
Méthodes héritées de la classe object
__delattr__,
__format__,
__getattribute__,
__reduce__,
__reduce_ex__,
__setattr__,
__str__
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :