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?

2
  • Pandas is pretty smart, if you don't care much about performance, you can just do pd.to_datetime(test['Timestamp'], errors='coerce')`. Commented Jul 14, 2021 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 Commented Jul 14, 2021 at 13:27

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.