hipdf.core.dtypes.CategoricalDtype#
25 min read time
- class hipdf.core.dtypes.CategoricalDtype(categories=None, ordered: bool = False)#
Bases:
_BaseDtypeType for categorical data with the categories and orderedness.
Parameters#
- categoriessequence, optional
Must be unique, and must not contain any nulls. The categories are stored in an Index, and if an index is provided the dtype of that index will be used.
- orderedbool or None, default False
Whether or not this categorical is treated as a ordered categorical. None can be used to maintain the ordered value of existing categoricals when used in operations that combine categoricals, e.g. astype, and will resolve to False if there is no existing ordered to maintain.
Attributes#
categories ordered
Methods#
from_pandas to_pandas
Examples#
>>> import cudf >>> dtype = cudf.CategoricalDtype(categories=['b', 'a'], ordered=True) >>> cudf.Series(['a', 'b', 'a', 'c'], dtype=dtype) 0 a 1 b 2 a 3 <NA> dtype: category Categories (2, object): ['b' < 'a']
Methods
__init__([categories, ordered])Return the array type associated with this dtype.
Construct this type from a string.
deserialize(header, frames)Generate an object from a serialized representation.
device_deserialize(header, frames)Perform device-side deserialization tasks.
Serialize data and metadata associated with device memory.
empty(shape)Construct an ExtensionArray of this dtype with the given shape.
from_pandas(dtype)Convert a
pandas.CategrocialDtypetocudf.CategoricalDtypehost_deserialize(header, frames)Perform device-side deserialization tasks.
Serialize data and metadata associated with host memory.
is_dtype(dtype)Check if we match 'dtype'.
Generate an equivalent serializable representation of an object.
Convert a
cudf.CategoricalDtypetopandas.CategoricalDtypeAttributes
An
Indexcontaining the unique categories allowed.A character code (one of 'biufcmMOSUV'), default 'O'
Default NA value to use for this type.
A string identifying the data type.
Ordered list of field names, or None if there are no fields.
Whether the categories have an ordered relationship.
The scalar type for the array, e.g.
int.- property categories: GenericIndex#
An
Indexcontaining the unique categories allowed.Examples#
>>> import cudf >>> dtype = cudf.CategoricalDtype(categories=['b', 'a'], ordered=True) >>> dtype.categories StringIndex(['b' 'a'], dtype='object')
- property type#
The scalar type for the array, e.g.
intIt’s expected
ExtensionArray[item]returns an instance ofExtensionDtype.typefor scalaritem, assuming that value is valid (not NA). NA values do not need to be instances of type.
- property name#
A string identifying the data type.
Will be used for display in, e.g.
Series.dtype
- property str#
- classmethod from_pandas(dtype: CategoricalDtype) CategoricalDtype#
Convert a
pandas.CategrocialDtypetocudf.CategoricalDtypeExamples#
>>> import cudf >>> import pandas as pd >>> pd_dtype = pd.CategoricalDtype(categories=['b', 'a'], ordered=True) >>> pd_dtype CategoricalDtype(categories=['b', 'a'], ordered=True) >>> cudf_dtype = cudf.CategoricalDtype.from_pandas(pd_dtype) >>> cudf_dtype CategoricalDtype(categories=['b', 'a'], ordered=True)
- to_pandas() CategoricalDtype#
Convert a
cudf.CategoricalDtypetopandas.CategoricalDtypeExamples#
>>> import cudf >>> dtype = cudf.CategoricalDtype(categories=['b', 'a'], ordered=True) >>> dtype CategoricalDtype(categories=['b', 'a'], ordered=True) >>> dtype.to_pandas() CategoricalDtype(categories=['b', 'a'], ordered=True)
- construct_from_string()#
Construct this type from a string.
This is useful mainly for data types that accept parameters. For example, a period dtype accepts a frequency parameter that can be set as
period[H](where H means hourly frequency).By default, in the abstract class, just the name of the type is expected. But subclasses can overwrite this method to accept parameters.
Parameters#
- stringstr
The name of the type, for example
category.
Returns#
- ExtensionDtype
Instance of the dtype.
Raises#
- TypeError
If a class cannot be constructed from this ‘string’.
Examples#
For extension dtypes with arguments the following may be an adequate implementation.
>>> @classmethod ... def construct_from_string(cls, string): ... pattern = re.compile(r"^my_type\[(?P<arg_name>.+)\]$") ... match = pattern.match(string) ... if match: ... return cls(**match.groupdict()) ... else: ... raise TypeError( ... f"Cannot construct a '{cls.__name__}' from '{string}'" ... )
- classmethod construct_array_type() type_t[ExtensionArray]#
Return the array type associated with this dtype.
Returns#
type
- empty(shape: Shape) type_t[ExtensionArray]#
Construct an ExtensionArray of this dtype with the given shape.
Analogous to numpy.empty.
Parameters#
shape : int or tuple[int]
Returns#
ExtensionArray
- classmethod is_dtype(dtype: object) bool#
Check if we match ‘dtype’.
Parameters#
- dtypeobject
The object to check.
Returns#
bool
Notes#
The default implementation is True if
cls.construct_from_string(dtype)is an instance ofcls.dtypeis an object and is an instance ofclsdtypehas adtypeattribute, and any of the above conditions is true fordtype.dtype.
- property kind: str#
A character code (one of ‘biufcmMOSUV’), default ‘O’
This should match the NumPy dtype used when the array is converted to an ndarray, which is probably ‘O’ for object if the extension type cannot be represented as a built-in NumPy type.
See Also#
numpy.dtype.kind