code: def test_apply(test_series):
      grouper = Grouper(freq="YE", label="right", closed="right")
  
      grouped = test_series.groupby(grouper)
  
      def f(x):
          return x.sort_values()[-3:]
  
      applied = grouped.apply(f)
      expected = test_series.groupby(lambda x: x.year).apply(f)
  
      applied.index = applied.index.droplevel(0)
      expected.index = expected.index.droplevel(0)
      tm.assert_series_equal(applied, expected)
  
imports: from datetime import datetime
  from operator import methodcaller
  import numpy
  import pytest
  import pandas
  from pandas import DataFrame, Index, Series, Timestamp
  import pandas._testing
  from pandas.core.groupby.grouper import Grouper
  from pandas.core.indexes.datetimes import date_range
label_desc: Use image node_id 2 for calling a global function with example usage: test_apply(test_series) and returns: x