Skip to content

Commit 15b6447

Browse files
committed
BUG: Keep column level name in resample nunique
Closes pandas-devgh-23222 xref pandas-devgh-23645
1 parent c986386 commit 15b6447

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

doc/source/reference/groupby.rst

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ application to columns of a specific data type.
9999
DataFrameGroupBy.idxmax
100100
DataFrameGroupBy.idxmin
101101
DataFrameGroupBy.mad
102+
DataFrameGroupBy.nunique
102103
DataFrameGroupBy.pct_change
103104
DataFrameGroupBy.plot
104105
DataFrameGroupBy.quantile

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ Groupby/Resample/Rolling
210210
^^^^^^^^^^^^^^^^^^^^^^^^
211211

212212
- 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`)
213+
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
213214
-
214215
-
215216

pandas/core/groupby/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ def groupby_series(obj, col=None):
15791579
from pandas.core.reshape.concat import concat
15801580
results = [groupby_series(obj[col], col) for col in obj.columns]
15811581
results = concat(results, axis=1)
1582+
results.columns.names = obj.columns.names
15821583

15831584
if not self.as_index:
15841585
results.index = ibase.default_index(len(results))

pandas/tests/groupby/test_function.py

+9
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,15 @@ def test_nunique_with_timegrouper():
897897
tm.assert_series_equal(result, expected)
898898

899899

900+
def test_nunique_preserves_column_level_names():
901+
# GH 23222
902+
test = pd.DataFrame([1, 2, 2],
903+
columns=pd.Index(['A'], name="level_0"))
904+
result = test.groupby([0, 0, 0]).nunique()
905+
expected = pd.DataFrame([2], columns=test.columns)
906+
tm.assert_frame_equal(result, expected)
907+
908+
900909
# count
901910
# --------------------------------
902911

pandas/tests/resample/test_datetime_index.py

+9
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,15 @@ def test_resample_nunique():
11351135
assert_series_equal(result, expected)
11361136

11371137

1138+
def test_resample_nunique_preserves_column_level_names():
1139+
# see gh-23222
1140+
df = tm.makeTimeDataFrame(freq="1D").abs()
1141+
df.columns = pd.MultiIndex.from_arrays([df.columns.tolist()] * 2,
1142+
names=["lev0", "lev1"])
1143+
result = df.resample("1h").nunique()
1144+
tm.assert_index_equal(df.columns, result.columns)
1145+
1146+
11381147
def test_resample_nunique_with_date_gap():
11391148
# GH 13453
11401149
index = pd.date_range('1-1-2000', '2-15-2000', freq='h')

0 commit comments

Comments
 (0)