GBR24 GBR24 - 4 months ago 12
Python Question

Vectorized date parsing in a pandas series?

I have a

pandas.core.series.Series
that looks like this:

import pandas as pd
s = pd.Series(["1/1/1900 8:00:00 AM", "1/1/1900 8:15:00 PM", "1/1/1900 9:02:11 PM"])


I'm trying to just parse out the time and AM/PM indicator, to get something like this:
8:00:00 AM
.

Here's what I have:

s.str.split()


Which yields:

0 ['1/1/1900', '8:00:00', 'AM']
1 ['1/1/1900', '8:15:00', 'PM']
2 ['1/1/1900', '9:02:11', 'PM']


From there, it's pretty trivial to do something like this to get what I want:

" ".join(s.str.split()[0][1:])


Which produces

'8:00:00 AM'


How can I vectorize this method, though? I have quite a few dates.

Answer

IIUC you can make an additional vectorised str.join call on the splitted strings:

In [141]:
s = pd.Series(["1/1/1900 8:00:00 AM", "1/1/1900 8:15:00 PM", "1/1/1900 9:02:11 PM"])
s.str.split().str[1:].str.join(" ")

Out[141]:
0    8:00:00 AM
1    8:15:00 PM
2    9:02:11 PM
dtype: object