Changing a Pandas column dtype: Use astype, not assignment

Changing a Pandas column dtype: Use astype, not assignment

Want to change the dtype of a Python Pandas series? You can’t — at least, not by assigning:

df['passenger_count'].dtype = 'int32'   # Error!

Instead, use “astype” to get a new series, and replace the old one:

df['passenger_count'] = df['passenger_count'].astype('int32')