Arnold Klein Arnold Klein - 11 months ago 48
Python Question

Simple way to extract date from text in Pandas

The excerpt from data:

Givent the following example of pandas dataframe:

df =
index date
7838 2012 January
7790 2012 January
7853 2015 September
7889 2016 March
7928 2015 October
7847 1999 January
7884 2006 January
7826 1992 January


Is there a simple (and pythonic) way to convert free text into a standard date time variable? Something like:

df =
index date
7838 2012-01-01
7790 2012-01-01
7853 2015-09-01
7889 2016-03-01
7928 2015-10-01
7847 1999-01-01
7884 2006-01-01
7826 1992-01-01

Answer Source

Use pd.to_datetime() to convert from text to date type. You can glean the appropriate date types from this list.

df['date'] = pd.to_datetime(df['date'], format='%Y %B')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download