Skip to content
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

BUG : ValueError in case on NaN value in groupby columns #24850

Merged
merged 7 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,8 @@ Groupby/Resample/Rolling
- Bug in :meth:`DataFrame.groupby` did not respect the ``observed`` argument when selecting a column and instead always used ``observed=False`` (:issue:`23970`)
- Bug in :func:`pandas.core.groupby.SeriesGroupBy.pct_change` or :func:`pandas.core.groupby.DataFrameGroupBy.pct_change` would previously work across groups when calculating the percent change, where it now correctly works per group (:issue:`21200`, :issue:`21235`).
- Bug preventing hash table creation with very large number (2^32) of rows (:issue:`22805`)
- Bug in groupby which casues ``ValueError`` if ``observed=True`` and ``nan`` is present in group column (:issue:`22805`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make these the same whatsnew entry. also this is specifically if grouping on a Categorical, pls say that.

- Bug in groupby which casues wrong grouping of categorical if ``observed=True`` and ``nan`` is present in group column (:issue:`21151`).

Reshaping
^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
self._labels = self.grouper.codes
if observed:
codes = algorithms.unique1d(self.grouper.codes)
codes = codes[codes != -1]
else:
codes = np.arange(len(categories))

Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,39 @@ def test_observed_groups(observed):
tm.assert_dict_equal(result, expected)


def test_observed_groups_with_nan(observed):
# GH 24740
df = pd.DataFrame({'cat': pd.Categorical(['a', np.nan, 'a'],
categories=['a', 'b', 'd']),
'vals': [1, 2, 3]})
g = df.groupby('cat', observed=observed)
result = g.groups
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the observed fixture here to test both cases

if observed:
expected = {'a': Index([0, 2], dtype='int64')}
else:
expected = {'a': Index([0, 2], dtype='int64'),
'b': Index([], dtype='int64'),
'd': Index([], dtype='int64')}
tm.assert_dict_equal(result, expected)


def test_dataframe_categorical_with_nan(observed):
# GH 21151
s1 = pd.Categorical([np.nan, 'a', np.nan, 'a'],
categories=['a', 'b', 'c'])
s2 = pd.Series([1, 2, 3, 4])
df = pd.DataFrame({'s1': s1, 's2': s2})
result = df.groupby('s1', observed=observed).first().reset_index()
if observed:
expected = DataFrame({'s1': pd.Categorical(['a'],
categories=['a', 'b', 'c']), 's2': [2]})
else:
expected = DataFrame({'s1': pd.Categorical(['a', 'b', 'c'],
categories=['a', 'b', 'c']),
's2': [2, np.nan, np.nan]})
tm.assert_frame_equal(result, expected)


def test_datetime():
# GH9049: ensure backward compatibility
levels = pd.date_range('2014-01-01', periods=4)
Expand Down