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 3 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.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,7 @@ 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 :meth: `pandas.core.groupby.groups` 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.

pandas.core.groupby.groups isn't a method. How about just "Bug in groupby causing a ValueError if ..."


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
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ def test_observed_groups(observed):
tm.assert_dict_equal(result, expected)


def test_observed_groups_with_nan():
# GH 24740
df = pd.DataFrame({'cat': pd.Categorical(['a', 'c', 'a'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you make this pd.Categorical(['a', np.nan, 'a'], categories=['a', 'b, 'd', 'e', 'f'])? It wasn't immediately obvious that ''c'` would end up as NA.

categories=['a', 'b', 'd', 'e', 'f']),
'vals': [1, 2, 3]})

g = df.groupby('cat', observed=True)
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

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


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