Open In App

Change String To Date In Pandas Dataframe

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Working with date and time data in data frame are common, sometimes date information is stored as strings in datasets and needs to be converted to proper date format for analysis and visualization purposes.

Change String to Date Format in Pandas DataFrame

In this article, we will explore how to convert string data to date format within a Pandas DataFrame. There are two different methods:

1. Using pandas.to_datetime()

We can convert string to date in a data frame with pandas.to_datatime()function.

Here, we create a data frame with two columns one with dates as a string and the other with random values, then we use the pandas to_datetime() function with the date column as the parameter. Finally, it converts the Date string column from a string to a DateTime object and creates a new column Date.

Python3




import pandas as pd
 
# Example Data
data = {
    'Date': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'Values': [100, 200, 300]
}
 
# Creating DataFrame
df = pd.DataFrame(data)
 
print("Before converting")
print(df.dtypes)
 
# Using pandas.to_datetime()
df['Date'] = pd.to_datetime(df['Date'])
 
print("\nAfter converting")
print(df.dtypes)


Output:

Before converting
Date object
Values int64
dtype: object
After converting
Date datetime64[ns]
Values int64
dtype: object

As you can observe, The ‘DateStrings’ column typically has an object (string) data type, and the ‘Values’ column has an integer data type. The ‘DateStrings’ column is not affected by the conversion operation, as the conversion result is stored in a new column ‘Date’.

2. Using DataFrame.astype()

We can use the pandas astype() function to convert string to date in the data frame.

Here, we create a data frame with two columns one with dates as a string and the other with random values, then we use the pandas astype() function with the date column. Finally, it converts the Date string column from a string to a DateTime object and creates a new column Date.

Python3




import pandas as pd
 
# Example Data
data = {
    'Date': ['2022-01-01', '2022-01-02', '2022-01-03'],
    'Values': [100, 200, 300]
}
 
# Creating DataFrame
df = pd.DataFrame(data)
 
print("Before converting")
print(df.dtypes)
 
# Using DataFrame.astype()
df['Date'] = df['Date'].astype('datetime64[ns]')
 
print("\nAfter converting")
print(df.dtypes)


Output:

Before converting
Date object
Values int64
dtype: object
After converting
Date datetime64[ns]
Values int64
dtype: object

As you can observe, The ‘DateStrings’ column typically has an object (string) data type, and the ‘Values’ column has an integer data type. The ‘DateStrings’ column is not affected by the conversion operation, as the conversion result is stored in a new column ‘Date’.

Conclusion

In conclusion, we discussed different methods to Change String To Date In Dataframe with pandas.to_datetime() and DataFrame.astype() functions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads