code: def infer_year(date):
      
      if isinstance(date, str):
          # Look for a string that begins with four numbers; the first four
          # numbers found are the year.
          pattern = r'(?P<year>\d{4})'
          result = re.match(pattern, date)
          if result:
              return int(result.groupdict()['year'])
          else:
              raise ValueError('Invalid date string provided: {}'.format(date))
      elif isinstance(date, np.datetime64):
          return date.item().year
      else:
          return date.year
docs: Given a datetime-like object or string infer the year.
  
      Parameters
      ----------
      date : datetime-like object or str
          Input date
  
      Returns
      -------
      int
  
      Examples
      --------
      >>> infer_year('2000')
      2000
      >>> infer_year('2000-01')
      2000
      >>> infer_year('2000-01-31')
      2000
      >>> infer_year(datetime.datetime(2000, 1, 1))
      2000
      >>> infer_year(np.datetime64('2000-01-01'))
      2000
      >>> infer_year(DatetimeNoLeap(2000, 1, 1))
      2000
      >>>