Federico_Lorenzo :
I have a timestamp column (Time) where the timestamp is in the following format
0 02.28.2020 10:21:26.0734 vorm.
1 02.28.2020 10:21:27.0720 vorm.
2 02.28.2020 10:21:30.0705 vorm.
3 02.28.2020 10:21:31.0742 vorm.
4 02.28.2020 10:21:32.0606 vorm.
3978 02.28.2020 11:30:11.1122 vorm.
3979 02.28.2020 11:30:12.1078 vorm.
3980 02.28.2020 11:30:13.1100 vorm.
3981 02.28.2020 11:30:14.1190 vorm.
3982 02.28.2020 11:30:15.1086 vorm.
Name: Time, Length: 3983, dtype: object
I tried to convert the string object in Timestamp object in the following manner:
df['Time'] = [datetime.datetime.strptime(d, "%d.%m.%Y %H:%M:%S.%f %p.") for d in df["Time"]]
But I get the following error:
ValueError: time data '02.28.2020 10:21:26.0734 vorm.' does not match format '%d.%m.%Y %H:%M:%S.%f %p.'
I assume that the error comes from "vorm.", which is the indication for "AM".
jezrael :
Use to_datetime
with change format to %H
to %I
for match hours in 12H format, also swapped %d
and %m
months with days and for correct parse AM
and PM
is necessary replace value in dict:
df['Time'] = pd.to_datetime(df["Time"].replace({'vorm.' :'AM'}, regex=True),
format="%m.%d.%Y %I:%M:%S.%f %p")
print (df)
Time
0 2020-02-28 10:21:26.073400
1 2020-02-28 10:21:27.072000
2 2020-02-28 10:21:30.070500
3 2020-02-28 10:21:31.074200
4 2020-02-28 10:21:32.060600
3978 2020-02-28 11:30:11.112200
3979 2020-02-28 11:30:12.107800
3980 2020-02-28 11:30:13.110000
3981 2020-02-28 11:30:14.119000
3982 2020-02-28 11:30:15.108600
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=291470&siteId=1