diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index f50052347cfb5..d39a17a01382d 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -340,6 +340,8 @@ Deprecations - ``pd.options.html.border`` has been deprecated in favor of ``pd.options.display.html.border`` (:issue:`15793`). +- :func:`SeriesGroupBy.nth` has deprecated ``True`` in favor of ``'all'`` for its kwarg ``dropna`` (:issue:`11038`). + .. _whatsnew_0210.prior_deprecations: Removal of prior version deprecations/changes diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 248f3b2095a78..f14ed08a27fae 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -1393,12 +1393,21 @@ def nth(self, n, dropna=None): return out.sort_index() if self.sort else out - if isinstance(self._selected_obj, DataFrame) and \ - dropna not in ['any', 'all']: - # Note: when agg-ing picker doesn't raise this, just returns NaN - raise ValueError("For a DataFrame groupby, dropna must be " - "either None, 'any' or 'all', " - "(was passed %s)." % (dropna),) + if dropna not in ['any', 'all']: + if isinstance(self._selected_obj, Series) and dropna is True: + warnings.warn("the dropna='%s' keyword is deprecated," + "use dropna='all' instead. " + "For a Series groupby, dropna must be " + "either None, 'any' or 'all'." % (dropna), + FutureWarning, + stacklevel=2) + dropna = 'all' + else: + # Note: when agg-ing picker doesn't raise this, + # just returns NaN + raise ValueError("For a DataFrame groupby, dropna must be " + "either None, 'any' or 'all', " + "(was passed %s)." % (dropna),) # old behaviour, but with all and any support for DataFrames. # modified in GH 7559 to have better perf diff --git a/pandas/tests/groupby/test_nth.py b/pandas/tests/groupby/test_nth.py index 28392537be3c6..ffbede0eb208f 100644 --- a/pandas/tests/groupby/test_nth.py +++ b/pandas/tests/groupby/test_nth.py @@ -2,7 +2,10 @@ import pandas as pd from pandas import DataFrame, MultiIndex, Index, Series, isna from pandas.compat import lrange -from pandas.util.testing import assert_frame_equal, assert_series_equal +from pandas.util.testing import ( + assert_frame_equal, + assert_produces_warning, + assert_series_equal) from .common import MixIn @@ -171,7 +174,10 @@ def test_nth(self): # doc example df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B']) g = df.groupby('A') - result = g.B.nth(0, dropna=True) + # PR 17493, related to issue 11038 + # test Series.nth with True for dropna produces DeprecationWarning + with assert_produces_warning(FutureWarning): + result = g.B.nth(0, dropna=True) expected = g.B.first() assert_series_equal(result, expected)