Participer au site avec un Tip
Rechercher
 

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 :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé
Module « pandas »

Classe « Categorical »

Informations générales

Héritage

builtins.object
    ABC
        BaseStringArrayMethods
            ObjectStringArrayMixin
    builtins.object
        DirNamesMixin
            PandasObject
    builtins.object
        ExtensionArray
    builtins.object
        NDArrayBacked
            NDArrayBackedExtensionArray
                Categorical

Définition

class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin):

help(Categorical)

Represent a categorical variable in classic R / S-plus fashion.

`Categoricals` can only take on 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)

Signature du constructeur Description
__init__(self, values, categories=None, ordered=None, dtype: 'Dtype | None' = None, fastpath: 'bool | lib.NoDefault' = <no_default>, copy: 'bool' = True) -> 'None'

Liste des attributs statiques

Attributs statiques hérités de la classe NDArrayBacked

nbytes, ndim, shape, size, T

Liste des propriétés

Nom de la propriétéDescription
categories
codes
dtype
nbytes
ordered

Propriétés héritées de la classe ExtensionArray

ndim, shape, size, T

Liste des opérateurs

Signature de l'opérateur Description
__contains__(self, key) -> 'bool'

Opérateurs hérités de la classe NDArrayBackedExtensionArray

__getitem__, __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
Signature de la méthodeDescription
__array__(self, dtype: 'NpDtype | None' = None, copy: 'bool | None' = None) -> 'np.ndarray'
__array_ufunc__(self, ufunc: 'np.ufunc', method: 'str', *inputs, **kwargs)
__iter__(self) -> 'Iterator'
__repr__(self) -> 'str'
__setstate__(self, state) -> 'None' Necessary for making this object picklable [extrait de __setstate__.__doc__]
add_categories(self, new_categories) -> 'Self'
argsort(self, *, ascending: 'bool' = True, kind: 'SortKind' = 'quicksort', **kwargs)
as_ordered(self) -> 'Self'
as_unordered(self) -> 'Self'
astype(self, dtype: 'AstypeArg', copy: 'bool' = True) -> 'ArrayLike'
check_for_ordered(self, op) -> 'None' assert that we are ordered [extrait de check_for_ordered.__doc__]
describe(self) -> 'DataFrame'
equals(self, other: 'object') -> 'bool'
from_codes(codes, categories=None, ordered=None, dtype: 'Dtype | None' = None, validate: 'bool' = True) -> 'Self'
isin(self, values: 'ArrayLike') -> 'npt.NDArray[np.bool_]'
isna(self) -> 'npt.NDArray[np.bool_]'
isnull(self) -> 'npt.NDArray[np.bool_]'
map(self, mapper, na_action: "Literal['ignore'] | None | lib.NoDefault" = <no_default>)
max(self, *, skipna: 'bool' = True, **kwargs)
memory_usage(self, deep: 'bool' = False) -> 'int'
min(self, *, skipna: 'bool' = True, **kwargs)
notna(self) -> 'npt.NDArray[np.bool_]'
notnull(self) -> 'npt.NDArray[np.bool_]'
remove_categories(self, removals) -> 'Self'
remove_unused_categories(self) -> 'Self'
rename_categories(self, new_categories) -> 'Self'
reorder_categories(self, new_categories, ordered=None) -> 'Self'
set_categories(self, new_categories, ordered=None, rename: 'bool' = False)
set_ordered(self, value: 'bool') -> 'Self'
sort_values(self, *, inplace: 'bool' = False, ascending: 'bool' = True, na_position: 'str' = 'last') -> 'Self | None'
to_list(self)
unique(self) -> 'Self'
value_counts(self, dropna: 'bool' = True) -> 'Series'

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

argmax, argmin, fillna, insert, searchsorted, shift, take, view

Méthodes héritées de la classe ExtensionArray

copy, delete, dropna, duplicated, factorize, interpolate, ravel, repeat, to_numpy, tolist, transpose

Méthodes héritées de la classe NDArrayBacked

__reduce__, __reduce_cython__, __setstate_cython__, reshape, swapaxes

Méthodes héritées de la classe object

__delattr__, __format__, __getattribute__, __getstate__, __hash__, __reduce_ex__, __setattr__, __str__

Vous êtes un professionnel et vous avez besoin d'une formation ? Coder avec une
Intelligence Artificielle
Voir le programme détaillé