Skip to content

CLN: True option in Series.groupby.nth(dropna=) #27168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ Removal of prior version deprecations/changes
- Removed the previously deprecated ``raise_on_error`` keyword argument in :meth:`DataFrame.where` and :meth:`DataFrame.mask` (:issue:`17744`)
- Removed the previously deprecated ``ordered`` and ``categories`` keyword arguments in ``astype`` (:issue:`17742`)
- Removed the previously deprecated ``cdate_range`` (:issue:`17691`)
- Removed the previously deprecated ``True`` option for the ``dropna`` keyword argument in :func:`SeriesGroupBy.nth` (:issue:`17493`)

.. _whatsnew_0250.performance:

Expand Down
22 changes: 5 additions & 17 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class providing the base-class of operations.
from functools import partial, wraps
import types
from typing import FrozenSet, List, Optional, Tuple, Type, Union
import warnings

import numpy as np

Expand Down Expand Up @@ -1741,22 +1740,11 @@ def nth(self,
"dropna option with a list of nth values is not supported")

if dropna not in ['any', 'all']:
if isinstance(self._selected_obj, Series) and dropna is True:
warnings.warn("the dropna={dropna} keyword is deprecated,"
"use dropna='all' instead. "
"For a Series groupby, dropna must be "
"either None, 'any' or 'all'.".format(
dropna=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 {dropna}).".format(
dropna=dropna))
# 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 {dropna}).".format(
dropna=dropna))

# old behaviour, but with all and any support for DataFrames.
# modified in GH 7559 to have better perf
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/groupby/test_nth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, isna
from pandas.util.testing import (
assert_frame_equal, assert_produces_warning, assert_series_equal)
from pandas.util.testing import assert_frame_equal, assert_series_equal


def test_first_last_nth(df):
Expand Down Expand Up @@ -168,13 +167,13 @@ def test_nth():
result = s.groupby(g, sort=False).nth(0, dropna='all')
assert_series_equal(result, expected)

with pytest.raises(ValueError, match='For a DataFrame groupby'):
s.groupby(g, sort=False).nth(0, dropna=True)

# doc example
df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B'])
g = df.groupby('A')
# PR 17493, related to issue 11038
# test Series.nth with True for dropna produces FutureWarning
with assert_produces_warning(FutureWarning):
result = g.B.nth(0, dropna=True)
result = g.B.nth(0, dropna='all')
expected = g.B.first()
assert_series_equal(result, expected)

Expand Down