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: Keep column level names during resample nunique (#23222) #23645

Closed
Closed
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/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,7 @@ application to columns of a specific data type.
DataFrameGroupBy.idxmax
DataFrameGroupBy.idxmin
DataFrameGroupBy.mad
DataFrameGroupBy.nunique
DataFrameGroupBy.pct_change
DataFrameGroupBy.plot
DataFrameGroupBy.quantile
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
- Bug in :meth:`DataFrame.expanding` in which the ``axis`` argument was not being respected during aggregations (:issue:`23372`)
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.transform` which caused missing values when the input function can accept a :class:`DataFrame` but renames it (:issue:`23455`).
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`).
dsm054 marked this conversation as resolved.
Show resolved Hide resolved

Reshaping
^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ def groupby_series(obj, col=None):
from pandas.core.reshape.concat import concat
results = [groupby_series(obj[col], col) for col in obj.columns]
results = concat(results, axis=1)
results.columns.names = obj.columns.names

if not self.as_index:
results.index = ibase.default_index(len(results))
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,15 @@ def test_nunique_with_timegrouper():
tm.assert_series_equal(result, expected)


def test_nunique_preserves_column_level_names():
# GH 23222
test = pd.DataFrame([1, 2, 2],
columns=pd.Index(['A'], name="level_0"))
result = test.groupby([0, 0, 0]).nunique()
expected = pd.DataFrame([2], columns=test.columns)
tm.assert_frame_equal(result, expected)


# count
# --------------------------------

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,14 @@ def test_resample_nunique(self):
result = df.ID.groupby(pd.Grouper(freq='D')).nunique()
assert_series_equal(result, expected)

def test_resample_nunique_preserves_column_level_names(self):
# GH 23222
df = tm.makeTimeDataFrame(freq='1D').abs()
df.columns = pd.MultiIndex.from_arrays([df.columns.tolist()] * 2,
names=["lev0", "lev1"])
result = df.resample("1h").nunique()
tm.assert_index_equal(df.columns, result.columns)

def test_resample_nunique_with_date_gap(self):
# GH 13453
index = pd.date_range('1-1-2000', '2-15-2000', freq='h')
Expand Down