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?
-
1So the df has a column that contains such lists?FObersteiner– FObersteiner2021-01-28 18:14:02 +00:00Commented Jan 28, 2021 at 18:14
-
In the first place, 18000000 is wrong: the number of microseconds must be < 1000000.DYZ– DYZ2021-01-28 18:22:30 +00:00Commented Jan 28, 2021 at 18:22
Add a comment
|
1 Answer
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))
))