0

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?

Ailurophile
  • 2,552
  • 7
  • 21
  • 46
  • Pandas is pretty smart, if you don't care much about performance, you can just do pd.to_datetime(test['Timestamp'], errors='coerce')`. – Quang Hoang Jul 14 '21 at 13:19
  • @QuangHoang This won't work with OP's example because of the dots. On pandas 1.1.5 without coercion to NaT: `ParserError: Unknown string format: 2021-03-19-17.03.19.149168` – Thrastylon Jul 14 '21 at 13:27

1 Answers1

1

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]
Thrastylon
  • 853
  • 7
  • 20