Skip to content

Commit a621b7c

Browse files
committed
DEPR: Add warning for True for dropna of SeriesGroupBy.nth
1 parent fdbc6b8 commit a621b7c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pandas/core/groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
compress_group_index, get_flattened_iterator,
5656
decons_obs_group_ids, get_indexer_dict)
5757
from pandas.util._decorators import (cache_readonly, Substitution,
58-
Appender, make_signature)
58+
Appender, make_signature, deprecate_kwarg)
5959
from pandas.io.formats.printing import pprint_thing
6060
from pandas.util._validators import validate_kwargs
6161

@@ -1296,6 +1296,8 @@ def backfill(self, limit=None):
12961296

12971297
@Substitution(name='groupby')
12981298
@Appender(_doc_template)
1299+
@deprecate_kwarg(old_arg_name='dropna', new_arg_name='dropna',
1300+
mapping={True: None})
12991301
def nth(self, n, dropna=None):
13001302
"""
13011303
Take the nth row from each group if n is an int, or a subset of rows

pandas/tests/groupby/test_nth.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import pandas as pd
33
from pandas import DataFrame, MultiIndex, Index, Series, isna
44
from pandas.compat import lrange
5-
from pandas.util.testing import assert_frame_equal, assert_series_equal
5+
from pandas.util.testing import (
6+
assert_frame_equal,
7+
assert_produces_warning,
8+
assert_series_equal)
69

710
from .common import MixIn
811

@@ -168,13 +171,6 @@ def test_nth(self):
168171
result = s.groupby(g, sort=False).nth(0, dropna='all')
169172
assert_series_equal(result, expected)
170173

171-
# doc example
172-
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
173-
g = df.groupby('A')
174-
result = g.B.nth(0, dropna=True)
175-
expected = g.B.first()
176-
assert_series_equal(result, expected)
177-
178174
# test multiple nth values
179175
df = DataFrame([[1, np.nan], [1, 3], [1, 4], [5, 6], [5, 7]],
180176
columns=['A', 'B'])
@@ -232,6 +228,17 @@ def test_nth_multi_index_as_expected(self):
232228
names=['A', 'B']))
233229
assert_frame_equal(result, expected)
234230

231+
def test_nth_dropna(self):
232+
# PR XXXXX, related to issue 16442
233+
# test nth with True for dropna produces DeprecationWarning
234+
# old doc example
235+
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
236+
g = df.groupby('A')
237+
with assert_produces_warning(FutureWarning):
238+
result = g.B.nth(0, dropna=True)
239+
expected = g.B.first()
240+
assert_series_equal(result, expected)
241+
235242

236243
def test_nth_empty():
237244
# GH 16064

0 commit comments

Comments
 (0)