def test_first_last_nth(self):
    grouped = self.df.groupby('A')
    first = grouped.first()
    expected = self.df.loc[([1, 0], ['B', 'C', 'D'])]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(first, expected)
    nth = grouped.nth(0)
    assert_frame_equal(nth, expected)
    last = grouped.last()
    expected = self.df.loc[([5, 7], ['B', 'C', 'D'])]
    expected.index = Index(['bar', 'foo'], name='A')
    assert_frame_equal(last, expected)
    nth = grouped.nth((- 1))
    assert_frame_equal(nth, expected)
    nth = grouped.nth(1)
    expected = self.df.loc[([2, 3], ['B', 'C', 'D'])].copy()
    expected.index = Index(['foo', 'bar'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(nth, expected)
    grouped['B'].first()
    grouped['B'].last()
    grouped['B'].nth(0)
    self.df.loc[((self.df['A'] == 'foo'), 'B')] = np.nan
    assert isnull(grouped['B'].first()['foo'])
    assert isnull(grouped['B'].last()['foo'])
    assert isnull(grouped['B'].nth(0)['foo'])
    df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
    g = df.groupby('A')
    result = g.first()
    expected = df.iloc[[1, 2]].set_index('A')
    assert_frame_equal(result, expected)
    expected = df.iloc[[1, 2]].set_index('A')
    result = g.nth(0, dropna='any')
    assert_frame_equal(result, expected)
def test_first_last_nth(self):
    grouped = self.df.groupby('A')
    first = grouped.first()
    expected = self.df.loc[([1, 0], ['B', 'C', 'D'])]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(first, expected)
    nth = grouped.nth(0)
    assert_frame_equal(nth, expected)
    last = grouped.last()
    expected = self.df.loc[([5, 7], ['B', 'C', 'D'])]
    expected.index = Index(['bar', 'foo'], name='A')
    assert_frame_equal(last, expected)
    nth = grouped.nth((- 1))
    assert_frame_equal(nth, expected)
    nth = grouped.nth(1)
    expected = self.df.loc[([2, 3], ['B', 'C', 'D'])].copy()
    expected.index = Index(['foo', 'bar'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(nth, expected)
    grouped['B'].first()
    grouped['B'].last()
    grouped['B'].nth(0)
    self.df.loc[((self.df['A'] == 'foo'), 'B')] = np.nan
    assert isnull(grouped['B'].first()['foo'])
    assert isnull(grouped['B'].last()['foo'])
    assert isnull(grouped['B'].nth(0)['foo'])
    df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
    g = df.groupby('A')
    result = g.first()
    expected = df.iloc[[1, 2]].set_index('A')
    assert_frame_equal(result, expected)
    expected = df.iloc[[1, 2]].set_index('A')
    result = g.nth(0, dropna='any')
    assert_frame_equal(result, expected)