@Substitution(name='<STR_LIT>')<EOL><INDENT>@Substitution(see_also=_common_see_also)<EOL>def nth(self, n, dropna=None):<DEDENT>
if isinstance(n, int):<EOL><INDENT>nth_values = [n]<EOL><DEDENT>elif isinstance(n, (set, list, tuple)):<EOL><INDENT>nth_values = list(set(n))<EOL>if dropna is not None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>nth_values = np.array(nth_values, dtype=np.intp)<EOL>self._set_group_selection()<EOL>if not dropna:<EOL><INDENT>mask_left = np.in1d(self._cumcount_array(), nth_values)<EOL>mask_right = np.in1d(self._cumcount_array(ascending=False) + <NUM_LIT:1>,<EOL>-nth_values)<EOL>mask = mask_left | mask_right<EOL>out = self._selected_obj[mask]<EOL>if not self.as_index:<EOL><INDENT>return out<EOL><DEDENT>ids, _, _ = self.grouper.group_info<EOL>out.index = self.grouper.result_index[ids[mask]]<EOL>return out.sort_index() if self.sort else out<EOL><DEDENT>if dropna not in ['<STR_LIT>', '<STR_LIT:all>']:<EOL><INDENT>if isinstance(self._selected_obj, Series) and dropna is True:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>dropna=dropna),<EOL>FutureWarning,<EOL>stacklevel=<NUM_LIT:2>)<EOL>dropna = '<STR_LIT:all>'<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>dropna=dropna))<EOL><DEDENT><DEDENT>max_len = n if n >= <NUM_LIT:0> else - <NUM_LIT:1> - n<EOL>dropped = self.obj.dropna(how=dropna, axis=self.axis)<EOL>if self.keys is None and self.level is None:<EOL><INDENT>axis = self.grouper.axis<EOL>grouper = axis[axis.isin(dropped.index)]<EOL><DEDENT>else:<EOL><INDENT>from pandas.core.groupby.grouper import _get_grouper<EOL>grouper, _, _ = _get_grouper(dropped, key=self.keys,<EOL>axis=self.axis, level=self.level,<EOL>sort=self.sort,<EOL>mutated=self.mutated)<EOL><DEDENT>grb = dropped.groupby(grouper, as_index=self.as_index, sort=self.sort)<EOL>sizes, result = grb.size(), grb.nth(n)<EOL>mask = (sizes < max_len).values<EOL>if len(result) and mask.any():<EOL><INDENT>result.loc[mask] = np.nan<EOL><DEDENT>if (len(self.obj) == len(dropped) or<EOL>len(result) == len(self.grouper.result_index)):<EOL><INDENT>result.index = self.grouper.result_index<EOL><DEDENT>else:<EOL><INDENT>result = result.reindex(self.grouper.result_index)<EOL><DEDENT>return result<EOL>
Take the nth row from each group if n is an int, or a subset of rows
if n is a list of ints.

If dropna, will take the nth non-null row, dropna is either
Truthy (if a Series) or 'all', 'any' (if a DataFrame);
this is equivalent to calling dropna(how=dropna) before the
groupby.

Parameters
----------
n : int or list of ints
    a single nth value for the row or a list of nth values
dropna : None or str, optional
    apply the specified dropna operation before counting which row is
    the nth row. Needs to be None, 'any' or 'all'
%(see_also)s
Examples
--------

>>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2],
...                    'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B'])
>>> g = df.groupby('A')
>>> g.nth(0)
     B
A
1  NaN
2  3.0
>>> g.nth(1)
     B
A
1  2.0
2  5.0
>>> g.nth(-1)
     B
A
1  4.0
2  5.0
>>> g.nth([0, 1])
     B
A
1  NaN
1  2.0
2  3.0
2  5.0

Specifying `dropna` allows count ignoring ``NaN``

>>> g.nth(0, dropna='any')
     B
A
1  2.0
2  3.0

NaNs denote group exhausted when using dropna

>>> g.nth(3, dropna='any')
    B
A
1 NaN
2 NaN

Specifying `as_index=False` in `groupby` keeps the original index.

>>> df.groupby('A', as_index=False).nth(1)
   A    B
1  1  2.0
4  2  5.0