Skip to content

Commit

Permalink
DEPR: Add warning for True for dropna of SeriesGroupBy.nth
Browse files Browse the repository at this point in the history
  • Loading branch information
tnir committed Sep 12, 2017
1 parent fdbc6b8 commit 72a8fda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 15 additions & 6 deletions pandas/core/groupby.py
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/groupby/test_nth.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 72a8fda

Please sign in to comment.