import pandas as pd # create a Series with float values ser = pd.Series([1.1, 2.2, 3.3]) # convert the Series to int ser = ser.astype(int) print(ser)
0 1 1 2 2 3 dtype: int64
import pandas as pd # create a Series with datetime strings ser = pd.Series(['2020-01-01', '2020-01-02', '2020-01-03']) # convert the Series to datetime ser = pd.to_datetime(ser) print(ser)
0 2020-01-01 1 2020-01-02 2 2020-01-03 dtype: datetime64[ns]In both examples, the astype method is used to convert a pandas Series object to a new data type. The first example converts a series of float values to int, while the second example converts a series of datetime strings to datetime format. The pandas package library is used in both of these example codes.