Convert Pandas Column to DateTime

To convert a Pandas column to a datetime format, you can use the to_datetime method. This method is part of the Pandas library and allows you to convert a column of strings to a datetime format.

Pandas Column to DateTime

import pandas as pd

df = pd.DataFrame({'date': ['05SEP2014:00:00:00.000']})

df['date'] = pd.to_datetime(df['date'], format='%d%b%Y:%H:%M:%S.%f')

print(df['date'])

Output:

The output of the above code shows that the date column has been converted to a datetime format.

0   2014-09-05
Name: date, dtype: datetime64[ns]

To filter based on a specific date, you can use the loc method of the DataFrame to select only the rows where the date column matches the desired date.

import pandas as pd

df = pd.DataFrame({'date': ['05SEP2014:00:00:00.000']})

df['date'] = pd.to_datetime(df['date'], format='%d%b%Y:%H:%M:%S.%f')

date_filter = df.loc[df['date'] == '2014-09-05']

print(date_filter)

Summary and Conclusion

In this article, we have learned how to convert pandas to datetime in python. I hope this was helpful. If you have any questions please comment below or reach out to me via my socials.

Leave a Comment

Scroll to Top