0

I have a df with a columns that contains, in two different string format, several timestamps.

These are the two formats:

fmt = "%a %b %d %H:%M:%S %Z%z %Y"
fmt_2 = "%a %b %d %H:%M:%S %Z %Y"

Thanks to this community, if I had just one type of string format I would be able to convert to datetime using

.apply(lambda x: datetime.strptime(x, fmt)

stamp = "Fri Oct 11 15:09:30 GMT+01:00 2019"
stamp_2 = 'Sun Oct 27 08:05:47 GMT 2019'
stamp_3 = 'Sat Oct 26 08:05:47 GMT 2019'
stamp_4 = "Sat Oct 12 15:09:30 GMT+01:00 2019"

#How I would convert the two format
#from string to datetime
#
fmt = "%a %b %d %H:%M:%S %Z%z %Y"
print(datetime.strptime(stamp, fmt))
fmt_2 = "%a %b %d %H:%M:%S %Z %Y"
print(datetime.strptime(stamp_2, fmt_2))

#building an example dataframe
question_dict = {"time":[stamp, stamp_2,
                           stamp_3,stamp_4],
                 "record":[1,2,3,4]}
question_df = pd.DataFrame(question_dict)
print(question_df.info())

How can I convert the elements in my column from string to datetime?

I tried to search but please let me know if my question is a duplicate.

5
  • I would suggest using pd.to_datetime twice (once for each format) on your column with errors="coerce". Example in this SO thread: stackoverflow.com/questions/47256212/… Commented Feb 15, 2023 at 16:01
  • @Chrysophylaxs thank you it is very similar to my problem, and I think my question is clearly a duplicate. I think can be useful to keep this question linking to the question you shared. Commented Feb 15, 2023 at 16:05
  • 1
    pd.to_datetime(df['time']) without a format would be enough. Commented Feb 15, 2023 at 16:10
  • 1
    Since you have mixed UTC offsets, you'll end up with data type 'object'. If you want the pandas datetime64 data type, use pd.to_datetime(df['time'], utc=True). Commented Feb 15, 2023 at 16:11
  • @FObersteiner @Quang Hoang thank you for the suggestions and for linking the right questions, I really appreciate that. I actually tried ` pd.to_datetime(df['time]'` before asking but it was giving me an object, while I wanted a datetime64 and I got confused :) Commented Feb 15, 2023 at 17:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.