Series.astype(dtype, copy=True, errors='raise', **kwargs)
[source]
Cast a pandas object to a specified dtype dtype
.
Parameters: |
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. copy : bool, default True. Return a copy when errors : {‘raise’, ‘ignore’}, default ‘raise’. Control raising of exceptions on invalid data for provided dtype.
New in version 0.20.0. raise_on_error : raise on invalid input Deprecated since version 0.20.0: Use
|
---|---|
Returns: |
|
See also
pandas.to_datetime
pandas.to_timedelta
pandas.to_numeric
numpy.ndarray.astype
>>> ser = pd.Series([1, 2], dtype='int32') >>> ser 0 1 1 2 dtype: int32 >>> ser.astype('int64') 0 1 1 2 dtype: int64
Convert to categorical type:
>>> ser.astype('category') 0 1 1 2 dtype: category Categories (2, int64): [1, 2]
Convert to ordered categorical type with custom ordering:
>>> ser.astype('category', ordered=True, categories=[2, 1]) 0 1 1 2 dtype: category Categories (2, int64): [2 < 1]
Note that using copy=False
and changing data on a new pandas object may propagate changes:
>>> s1 = pd.Series([1,2]) >>> s2 = s1.astype('int64', copy=False) >>> s2[0] = 10 >>> s1 # note that s1[0] has changed too 0 10 1 2 dtype: int64
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
http://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.Series.astype.html