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 - Create a DataFrame:

Python-Pandas Code:

import numpy as np
import pandas as pd
d = {'c1': [2, 3], 'c2': [4, 5]}
df = pd.DataFrame(data=d)
df.dtypes

Output:

c1    int64
c2    int64
dtype: object
Pandas Series astype() function

Example - Cast all columns to int32:

Python-Pandas Code:

import numpy as np
import pandas as pd
d = {'c1': [2, 3], 'c2': [4, 5]}
df = pd.DataFrame(data=d)
df.astype('int32').dtypes

Output:

c1    int32
c2    int32
dtype: object
Pandas Series astype() function

Example - Cast c1 to int32 using a dictionary:

Python-Pandas Code:

import numpy as np
import pandas as pd
d = {'c1': [2, 3], 'c2': [4, 5]}
df = pd.DataFrame(data=d)
df.astype({'c1': 'int32'}).dtypes

Output:

c1    int32
c2    int64
dtype: object

Example - Create a series:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], dtype='int32')
s

Output:

0    2
1    3
dtype: int32
Pandas Series astype() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], dtype='int32')
s.astype('int64')

Output:

0    2
1    3
dtype: int64
Pandas Series astype() function

Example - Convert to categorical type:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], dtype='int32')
s.astype('category')

Output:

0    2
1    3
dtype: category
Categories (2, int64): [2, 3]
Pandas Series astype() function

Example - Convert to ordered categorical type with custom ordering:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([2, 3], dtype='int32')
cat_dtype = pd.api.types.CategoricalDtype(
                    categories=[3, 2], ordered=True)
s.astype(cat_dtype)

Output:

0    2
1    3
dtype: category
Categories (2, int64): [3 < 2]

Example - Note that using copy=False and changing data on a new pandas object may propagate changes:

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series([3,2])
s2 = s1.astype('int64', copy=False)
s2[0] = 10
s1  # note that s1[0] has changed too

Output:

0    10
1     2
dtype: int64

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



Follow us on Facebook and Twitter for latest update.