Skip to content

Commit 8dd1d36

Browse files
committed
BUG: Keep column level names during resample nunique (pandas-dev#23222)
Closes pandas-dev#23222
1 parent 2d4dd50 commit 8dd1d36

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,7 @@ application to columns of a specific data type.
23462346
DataFrameGroupBy.idxmax
23472347
DataFrameGroupBy.idxmin
23482348
DataFrameGroupBy.mad
2349+
DataFrameGroupBy.nunique
23492350
DataFrameGroupBy.pct_change
23502351
DataFrameGroupBy.plot
23512352
DataFrameGroupBy.quantile

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ Groupby/Resample/Rolling
13351335
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
13361336
- Bug in :meth:`DataFrame.expanding` in which the ``axis`` argument was not being respected during aggregations (:issue:`23372`)
13371337
- 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`).
1338+
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`).
13381339

13391340
Reshaping
13401341
^^^^^^^^^

pandas/core/groupby/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ def groupby_series(obj, col=None):
15681568
from pandas.core.reshape.concat import concat
15691569
results = [groupby_series(obj[col], col) for col in obj.columns]
15701570
results = concat(results, axis=1)
1571+
results.columns.names = obj.columns.names
15711572

15721573
if not self.as_index:
15731574
results.index = ibase.default_index(len(results))

pandas/tests/groupby/test_function.py

+9
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,15 @@ def test_nunique_with_timegrouper():
892892
tm.assert_series_equal(result, expected)
893893

894894

895+
def test_nunique_preserves_column_level_names():
896+
# GH 23222
897+
test = pd.DataFrame([1, 2, 2],
898+
columns=pd.Index(['A'], name="level_0"))
899+
result = test.groupby([0, 0, 0]).nunique()
900+
expected = pd.DataFrame([2], columns=test.columns)
901+
tm.assert_frame_equal(result, expected)
902+
903+
895904
# count
896905
# --------------------------------
897906

pandas/tests/test_resample.py

+8
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,14 @@ def test_resample_nunique(self):
19471947
result = df.ID.groupby(pd.Grouper(freq='D')).nunique()
19481948
assert_series_equal(result, expected)
19491949

1950+
def test_resample_nunique_preserves_column_level_names(self):
1951+
# GH 23222
1952+
df = tm.makeTimeDataFrame(freq='1D').abs()
1953+
df.columns = pd.MultiIndex.from_arrays([df.columns.tolist()] * 2,
1954+
names=["lev0", "lev1"])
1955+
result = df.resample("1h").nunique()
1956+
tm.assert_index_equal(df.columns, result.columns)
1957+
19501958
def test_resample_nunique_with_date_gap(self):
19511959
# GH 13453
19521960
index = pd.date_range('1-1-2000', '2-15-2000', freq='h')

0 commit comments

Comments
 (0)