cudf.Series.astype#
- Series.astype(dtype, copy: bool = False, errors: Literal['raise', 'ignore'] = 'raise')[source]#
Cast the object to the given dtype.
- Parameters:
- dtypedata type, or dict of column name -> data type
Use a
numpy.dtype
or Python type to cast entire DataFrame object to the same type. Alternatively, use{col: dtype, ...}
, where col is a column label and dtype is anumpy.dtype
or Python type to cast one or more of the DataFrame’s columns to column-specific types.- copybool, default False
Return a deep-copy when
copy=True
. Note by defaultcopy=False
setting is used and hence changes to values then may propagate to other cudf objects.- errors{‘raise’, ‘ignore’, ‘warn’}, default ‘raise’
Control raising of exceptions on invalid data for provided dtype.
raise
: allow exceptions to be raisedignore
: suppress exceptions. On error return original object.
- Returns:
- DataFrame/Series
Examples
DataFrame
>>> import cudf >>> df = cudf.DataFrame({'a': [10, 20, 30], 'b': [1, 2, 3]}) >>> df a b 0 10 1 1 20 2 2 30 3 >>> df.dtypes a int64 b int64 dtype: object
Cast all columns to int32:
>>> df.astype('int32').dtypes a int32 b int32 dtype: object
Cast a to float32 using a dictionary:
>>> df.astype({'a': 'float32'}).dtypes a float32 b int64 dtype: object >>> df.astype({'a': 'float32'}) a b 0 10.0 1 1 20.0 2 2 30.0 3
Series
>>> import cudf >>> series = cudf.Series([1, 2], dtype='int32') >>> series 0 1 1 2 dtype: int32 >>> series.astype('int64') 0 1 1 2 dtype: int64
Convert to categorical type:
>>> series.astype('category') 0 1 1 2 dtype: category Categories (2, int64): [1, 2]
Convert to ordered categorical type with custom ordering:
>>> cat_dtype = cudf.CategoricalDtype(categories=[2, 1], ordered=True) >>> series.astype(cat_dtype) 0 1 1 2 dtype: category Categories (2, int64): [2 < 1]
Note that using
copy=False
(enabled by default) and changing data on a new Series will propagate changes:>>> s1 = cudf.Series([1, 2]) >>> s1 0 1 1 2 dtype: int64 >>> s2 = s1.astype('int64', copy=False) >>> s2[0] = 10 >>> s1 0 10 1 2 dtype: int64