Let’s see how we can convert a column of data rows (in dd / mm / yyyy format) to date and time format. We cannot perform any time series operations with dates if they are not formatted correctly. To be able to work with it, we must convert dates to datetime format.
Code # 1: Convert the Pandas data column type from string format to datetime format using pd .to_datetime ()
.
|
Output:
As we can see in the output, the data type of the "Date" column is — it is an object, that is, a string. We now convert it to datetime format using pd.to_datetime ()
.
|
Output:
As we can see in the output, the Date column format has been changed to date and time format.
Code # 2: Convert the Pandas data column type from strings and into date and time format using DataFrame.astype ()
.
|
Output:
As we can see in the output, the data type of the Date column is — it is an object, that is, a string. We now convert it to datetime format using DataFrame.astype ()
.
|
Output:
As we can see in the output, the format of the Date column has been changed to date and time.