pythonpandasdata-sciencedata-cleaningdata-scrubbing

Pandas: How can I convert 'timestamp' values in my dataframe column from object/str to timestamp?


My timestamp looks like below in the dataframe of my column but it is in 'object'. I want to convert this into 'timestamp'. How can I convert all values such in my dataframe column into timestamp?

0    01/Jul/1995:00:00:01
1    01/Jul/1995:00:00:06
2    01/Jul/1995:00:00:09
3    01/Jul/1995:00:00:09
4    01/Jul/1995:00:00:09
Name: timestamp, dtype: object

I tried below code referring this stackoverflow post but it gives me error:

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

Below is the error:

ValueError: time data '01/Jul/1995:00:00:01' does not match format '%d/%b/%Y:%H:%M:%S.%f' (match)

Solution

  • Try the follwing format:

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