[Python 3.5] Regex on a DataFrame column

I have a dataframe column df['Time'] it is a dtype O (I think that means it's an object) column (i.e. 41:23:00) and I just want to grab everything that comes before the first colon.

I have had a lot of trouble turning this dtype into a string to use for a split and the regex re.search(r'^[^:]+:\s*', df['Time']) is not getting me what I need .

Any help would be appreciated

Edit: using re.search(r'^[^:]+(?=:)' ,df['Time'].str) I get the error TypeError: expected string or bytes-like object

Posted Jun 12, 2017, 2:39 AM UTC · edited

12
ouichuatrnh•

You're probably going to want to map over every element of the column^*, but have you tried something like str(df['Time'][0])?

*edit: depending on what you intend to do

1
inbetweenaccounts•
df['time'] = df['time'].str.split(':').str[0]

Also put a backslash in front of your ^ to make it not raise everything.

1