pandas.Series.astype¶
Cast a pandas object to a specified dtype dtype
.
- param dtype
- data type, or dict of column name -> data type
Use a numpy.dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types.
- param copy
- bool, default True
Return a copy when
copy=True
(be very careful settingcopy=False
as changes to values then may propagate to other pandas objects).
- param errors
- {‘raise’, ‘ignore’}, 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
New in version 0.20.0.
- param kwargs
keyword arguments to pass on to the constructor
- return
casted : same type as caller
Limitations¶
Currently copy=False is not supported
Examples¶
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_astype():
series = pd.Series([3, -10, np.nan, 0, 92])
return series.astype(str)
print(series_astype())
$ python ./series/series_astype.py
0 3.000000
1 -10.000000
2 nan
3 0.000000
4 92.000000
dtype: object
See also
Convert argument to datetime.
Convert argument to timedelta.
Convert argument to a numeric type.
Copy of the array, cast to a specified type.