Skip to content

Commit 72a8fda

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

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

pandas/core/groupby.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1393,12 +1393,21 @@ def nth(self, n, dropna=None):
13931393

13941394
return out.sort_index() if self.sort else out
13951395

1396-
if isinstance(self._selected_obj, DataFrame) and \
1397-
dropna not in ['any', 'all']:
1398-
# Note: when agg-ing picker doesn't raise this, just returns NaN
1399-
raise ValueError("For a DataFrame groupby, dropna must be "
1400-
"either None, 'any' or 'all', "
1401-
"(was passed %s)." % (dropna),)
1396+
if dropna not in ['any', 'all']:
1397+
if isinstance(self._selected_obj, Series) and dropna is True:
1398+
warnings.warn("the dropna='%s' keyword is deprecated,"
1399+
"use dropna='all' instead. "
1400+
"For a Series groupby, dropna must be "
1401+
"either None, 'any' or 'all'." % (dropna),
1402+
FutureWarning,
1403+
stacklevel=2)
1404+
dropna = 'all'
1405+
else:
1406+
# Note: when agg-ing picker doesn't raise this,
1407+
# just returns NaN
1408+
raise ValueError("For a DataFrame groupby, dropna must be "
1409+
"either None, 'any' or 'all', "
1410+
"(was passed %s)." % (dropna),)
14021411

14031412
# old behaviour, but with all and any support for DataFrames.
14041413
# modified in GH 7559 to have better perf

pandas/tests/groupby/test_nth.py

+8-2
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

@@ -171,7 +174,10 @@ def test_nth(self):
171174
# doc example
172175
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
173176
g = df.groupby('A')
174-
result = g.B.nth(0, dropna=True)
177+
# PR 17493, related to issue 11038
178+
# test Series.nth with True for dropna produces DeprecationWarning
179+
with assert_produces_warning(FutureWarning):
180+
result = g.B.nth(0, dropna=True)
175181
expected = g.B.first()
176182
assert_series_equal(result, expected)
177183

0 commit comments

Comments
 (0)