@@ -684,6 +684,7 @@ def value_counts(
684
684
685
685
from pandas .core .reshape .tile import cut
686
686
687
+ """
687
688
if bins is not None and not np.iterable(bins):
688
689
# scalar bins cannot be done at top level
689
690
# in a backward compatible way
@@ -694,6 +695,7 @@ def value_counts(
694
695
ascending=ascending,
695
696
bins=bins,
696
697
)
698
+ """
697
699
ids , _ , _ = self .grouper .group_info
698
700
val = self .obj ._values
699
701
codes = self .grouper .reconstructed_codes # this will track the groups
@@ -735,14 +737,9 @@ def value_counts(
735
737
val_lab = val_lab [changes ]
736
738
ids = ids [changes ]
737
739
cts = np .diff (np .nonzero (np .r_ [changes , True ]))[0 ]
738
-
739
740
idx = np .r_ [0 , 1 + np .nonzero (change_ids )[0 ]]
741
+ # how many times each index gets repeated
740
742
rep = partial (np .repeat , repeats = np .add .reduceat (changes , idx ))
741
- num_repeats = np .diff (np .nonzero (np .r_ [True , change_ids , True ]))[0 ]
742
-
743
- change_ids = np .r_ [ # need to update now that we removed full repeats
744
- ids [1 :] != ids [:- 1 ], True
745
- ]
746
743
747
744
if (not dropna ) and (- 1 in val_lab ):
748
745
# in this case we need to explicitly add NaN as a level
@@ -755,6 +752,7 @@ def value_counts(
755
752
names = self .grouper .names + [self ._selection_name ]
756
753
757
754
if normalize :
755
+ num_repeats = np .diff (idx , append = len (ids ))
758
756
cts = cts .astype ("float" )
759
757
cts /= rep (
760
758
num_repeats
@@ -784,6 +782,9 @@ def value_counts(
784
782
ncat = len (codes [0 ])
785
783
fout = np .zeros ((ncat * nbin ), dtype = float if normalize else np .int64 )
786
784
id = 0
785
+ change_ids = np .r_ [ # need to update now that we removed full repeats
786
+ ids [1 :] != ids [:- 1 ], True
787
+ ]
787
788
for i , ct in enumerate (cts ): # fill in nonzero values of fout
788
789
fout [id * nbin + val_lab [i ]] = cts [i ]
789
790
id += change_ids [i ]
0 commit comments