0

I have a dataframe of transportation data. The datetime fields are logged in this format: [2020, 12, 10, 15, 0, 5, 18000000]. How do I parse these as datetime objects?

2
  • 1
    So the df has a column that contains such lists? Commented Jan 28, 2021 at 18:14
  • In the first place, 18000000 is wrong: the number of microseconds must be < 1000000. Commented Jan 28, 2021 at 18:22

1 Answer 1

1

You can pass it with * to the datetime.datetime constructor and use any pandas function to apply this to every value in your pandas.Series.

>>> from datetime import datetime
>>> datetime(*[2020, 12, 10, 15, 0, 5, 18000])
datetime.datetime(2020, 12, 10, 15, 0, 5, 18000)

One additional moment: you will need to update microsecond field. E.g.

from datetime import datetime

import pandas as pd

df = pd.DataFrame({"example": [[2020, 12, 10, 15, 0, 5, 18000000]]})
df.example = df.example.apply(lambda x: datetime(
    *(v if i != len(x) - 1 else v // 1000 for i, v in enumerate(x))
))
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.