I do realize this has already been addressed here. Nevertheless, I hope this question was different.
I have a dataframe
with timestamp with dtype as object
.
sample = {"Timestamp" : ["2021-03-19-17.03.19.149168", "2021-02-26-17.03.40.062637",
"2021-02-26-17.00.35.580631", "2021-06-09-18.03.38.497239",
"2021-06-09-18.03.38.497239"]}
test = pd.DataFrame(sample)
I'm trying to convert the object type to pandas datetime
,
test["Timestamp"] = pd.to_datetime(test["Timestamp"],
format="%Y-%m-%d-%H.%M.%S.%Z",
errors="coerce")
Using the above code returns
Timestamp
0 NaT
1 NaT
2 NaT
3 NaT
4 NaT
How to convert the above timestamp to pandas datetime
type?
Your format is incorrect, you are using %Z
(timezone) instead of %f
(microseconds). The latter works as expected:
>>> pd.to_datetime(test["Timestamp"], format="%Y-%m-%d-%H.%M.%S.%f")
0 2021-03-19 17:03:19.149168
1 2021-02-26 17:03:40.062637
2 2021-02-26 17:00:35.580631
3 2021-06-09 18:03:38.497239
4 2021-06-09 18:03:38.497239
Name: Timestamp, dtype: datetime64[ns]