Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Series: astype() function

Change data type of a series in Pandas

The astype() function is used to cast a pandas object to a specified data type.

Syntax:

Series.astype(self, dtype, copy=True, errors='raise', **kwargs)
Pandas Series astype() function

Parameters:

Name Description Type/Default Value Required / Optional
dtype  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.

data type, or dict of column name -> data type Required
copy  Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects). bool
Default Value: True
Required
errors

Control raising of exceptions on invalid data for provided dtype.

  • raise : allow exceptions to be raised
  • ignore : suppress exceptions. On error return original object
{‘raise’, ‘ignore’}
Default Value: ‘raise’
Required
kwargs  keyword arguments to pass on to the constructor    

Returns: casted - same type as caller

Example:


Download the Pandas Series Notebooks from here.

Previous: Memory usage of Pandas Series
Next: Better dtypes for object columns