Bimons
Bimons

Reputation: 241

Extracting a date at the end of a pandas dataframe

I have a column of data that is in the following format:

bla bla bla, bla bla bla, bla bla bla, bla bla bla bla bla bla, 23/09/2012

Is there an easy way to extract the date at the end of the string from all rows? It is in the same DD/MM/YYYY format every time.

I could split on the last , but it would be good if I could specify that I want the date in case of errors in the data.

Upvotes: 0

Views: 142

Answers (3)

Vaishali
Vaishali

Reputation: 38415

Use str.extract if you are not sure about the date being at the end of the string

df['date'] = df['col'].str.extract('(\d{2}/\d{2}/\d{4})', expand = False)

0    23/09/2012

Upvotes: 1

jpp
jpp

Reputation: 164623

You can use string slicing followed by pd.to_datetime. For example:

df['col'] = pd.to_datetime(df['col'].str[-10:], dayfirst=True)

Upvotes: 1

Benoît P
Benoît P

Reputation: 3265

row.split(", ")[-1] is the string 23/09/2012, now you can use the date module to extract that into a Date object. And use try/except for error handling.

Upvotes: 1

Related Questions