Skip to content

Commit

Permalink
BUG: Keep column level name in resample nunique (#25469)
Browse files Browse the repository at this point in the history
Closes gh-23222

xref gh-23645
  • Loading branch information
gfyoung authored and jreback committed Feb 28, 2019
1 parent 70802c2 commit 3b570e3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/reference/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug in :meth:`pandas.core.resample.Resampler.agg` with a timezone aware index where ``OverflowError`` would raise when passing a list of functions (:issue:`22660`)
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
-
-

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 @@ -1579,6 +1579,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 @@ -897,6 +897,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
9 changes: 9 additions & 0 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,15 @@ def test_resample_nunique():
assert_series_equal(result, expected)


def test_resample_nunique_preserves_column_level_names():
# see 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():
# GH 13453
index = pd.date_range('1-1-2000', '2-15-2000', freq='h')
Expand Down

0 comments on commit 3b570e3

Please sign in to comment.