Skip to content

Commit 7a0b22f

Browse files
committed
BUG: make construction of labels can handle empty periods (pandas-dev#28479)
* If applying rep to recons_labels go fail, use ids which has no consecutive duplicates instead.
1 parent 3f0e816 commit 7a0b22f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pandas/core/groupby/generic.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1264,9 +1264,16 @@ def value_counts(
12641264

12651265
# num. of times each group should be repeated
12661266
rep = partial(np.repeat, repeats=np.add.reduceat(inc, idx))
1267-
1268-
# multi-index components
1269-
labels = list(map(rep, self.grouper.recons_labels)) + [llab(lab, inc)]
1267+
1268+
#multi-index components
1269+
try:
1270+
labels = list(map(rep, self.grouper.recons_labels )) + [llab(lab, inc)]
1271+
except ValueError:
1272+
# If applying rep to recons_labels go fail, use ids which has no
1273+
# consecutive duplicates instead.
1274+
_ids_idx = np.ones(len(ids), dtype=bool)
1275+
_ids_idx[1:] = ids[1:] != ids[:-1]
1276+
labels = list(map(rep, [ids[_ids_idx]])) + [llab(lab, inc)]
12701277
levels = [ping.group_index for ping in self.grouper.groupings] + [lev]
12711278
names = self.grouper.names + [self._selection_name]
12721279

0 commit comments

Comments
 (0)