Q:

Year extraction with datetime library for several columns

I want to create a function that goes through my Pandas Dataframe and converts all date variables in the following format: %b-%Y, for example: Jun-2017, Apr-2017, May-2017, and so on, into the difference between the current year and the year of the date in each registry (for example: 2018 - year_corresponding_to_each_registry).
My function is:
def date_to_numeric(df):
  for col in dates:
    print("Converting date variables to years: " + col, flush = True)
    df[col] = df[col].apply(pd.to_datetime, format = '%b-%Y', errors = 'coerce')     
    df[col] = df[col].apply(dt.year)
  return df

I am having a problem in the following line:
df[col] = df[col].apply(dt.year)

I am receiving the following error message:
AttributeError: module 'datetime' has no attribute 'year'

P.D. I have imported datetime library with the following line code:
import datetime as dt

Thanks in advance for any help provided in how to accomplish this in an efficient way. 

ANSWER

Thanks to @DeepSpace answer (check it below marked as correct answer):
def date_to_numeric(df):
  for col in dates:
    print("Converting date variables to years: " + col, flush = True)
    df[col] = df[col].apply(pd.to_datetime, format = '%b-%Y', errors = 'coerce')     
    df[col] = 2018 - df[col].apply(pd.to_datetime, errors = 'coerce').dt.year
  return df

A:

Your code is overly complicated even if it would have worked.
df = pd.DataFrame({'a': ['Jun-2017', 'Apr-2017', 'Jan-2015']})

print(df)

#            a
#   0  Jun-2017
#   1  Apr-2017
#   2  Jan-2015

df['b'] = 2018 - pd.to_datetime(df['a']).dt.year

print(df)

#             a  b
#   0  Jun-2017  1
#   1  Apr-2017  1
#   2  Jan-2015  3

This will work column wise (ie needs to be executed on each column separately) and will only work if all elements in the column can be parsed as a date. If this is not the case with your actual data you will need to take that into account (for example by passing errors='coerce' to pd.to_datetime:
df = pd.DataFrame({'a': ['Jun-2017', 'Apr-2017', 'Jan-2015', 'i am not a date']})
df['b'] = 2018 - pd.to_datetime(df['a'], errors='coerce').dt.year

print(df)
#                   a    b
#  0         Jun-2017  1.0
#  1         Apr-2017  1.0
#  2         Jan-2015  3.0
#  3  i am not a date  NaN

