Q:

How to extract year (or datetime) from a column in a pandas dataframe that contains text

Suppose I have a pandas dataframe: 
Id    Book                      
1     Harry Potter (1997)
2     Of Mice and Men (1937)
3     Babe Ruth Story, The (1948)   Drama   948)    Babe Ruth Story

How do I extract the year from the column?
Output should be:
Id    Book Title               Year
1     Harry Potter             1997
2     Of Mice and Men          1937
3     Babe Ruth Story, The     1948

So far I have tried:
movies['year'] = movies['title'].str.extract('([0-9(0-9)]+)', expand=False).str.strip()

and
books['year'] = books['title'].str[-5:-1]

I've messed around with some other things and haven't gotten it to work yet. Any suggestions?

A:

How about a simple Regex:
text = 'Harry Potter (1997)'
re.findall('\((\d{4})\)', text)
# ['1997'] Note that this is a list of "all" the occurrences.

With a Dataframe, it can be done like this:
text = 'Harry Potter (1997)'
df = pd.DataFrame({'Book': text}, index=[1])
pattern = '\((\d{4})\)'
df['year'] = df.Book.str.extract(pattern, expand=False) #False returns a series

df
#                  Book   year
# 1  Harry Potter (1997)  1997

Finally, if you actually want to separate the title and the data (taking the dataframe reconstruction from Philip in another answer):
df = pd.DataFrame(columns=['Book'], data=[['Harry Potter (1997)'],['Of Mice and Men (1937)'],['Babe Ruth Story, The (1948)   Drama   948)    Babe Ruth Story']])

sep = df['Book'].str.extract('(.*)\((\d{4})\)', expand=False)

sep # A new df, separated into title and year
#                       0      1                           
# 0          Harry Potter   1997 
# 1       Of Mice and Men   1937
# 2  Babe Ruth Story, The   1948

