Skip to content

Calculate normalized freqs in value_counts correctly when bins is not None #26923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,16 +730,14 @@ def value_counts(

# count, remove nulls (from the index), and but the bins
result = ii.value_counts(dropna=dropna)
result = result[result.index.notna()]
result.index = result.index.astype("interval")
result.index = result.index.astype('interval')
result = result.sort_index()

# if we are dropna and we have NO values
if dropna and (result.values == 0).all():
result = result.iloc[0:0]

# normalizing is by len of all (regardless of dropna)
counts = np.array([len(ii)])
counts = np.array([result.sum()])

else:

Expand Down
30 changes: 18 additions & 12 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,21 +1035,27 @@ def test_dropna(self):
expected = Series([2, 1, 1], index=[5.0, 10.3, np.nan])
tm.assert_series_equal(result, expected)

def test_value_counts_normalized(self):
@pytest.mark.parametrize('dropna, vals, index', [
(False, [0.6, 0.2, 0.2], [np.nan, 2.0, 1.0]),
(True, [0.5, 0.5], [2.0, 1.0])])
@pytest.mark.parametrize('dtype', [np.float64, np.object, 'M8[ns]'])
def test_value_counts_normalized(self, dropna, vals, index, dtype):
# GH12558
s = Series([1, 2, np.nan, np.nan, np.nan])
dtypes = (np.float64, np.object, "M8[ns]")
for t in dtypes:
s_typed = s.astype(t)
result = s_typed.value_counts(normalize=True, dropna=False)
expected = Series(
[0.6, 0.2, 0.2], index=Series([np.nan, 2.0, 1.0], dtype=t)
)
tm.assert_series_equal(result, expected)
s_typed = s.astype(dtype)
result = s_typed.value_counts(normalize=True, dropna=dropna)
expected = Series(vals, index=Series(index, dtype=dtype))
tm.assert_series_equal(result, expected)

result = s_typed.value_counts(normalize=True, dropna=True)
expected = Series([0.5, 0.5], index=Series([2.0, 1.0], dtype=t))
tm.assert_series_equal(result, expected)
@pytest.mark.parametrize('dropna, vals, tuples', [
(False, [0.5, 0.3, 0.2], [(-0.005, 2.0), (2.0, 4.0), np.nan]),
(True, [0.625, 0.375], [(-0.005, 2.0), (2.0, 4.0)])])
def test_value_counts_normalized_bins(self, dropna, vals, tuples):
# GH25970
s = Series([1, 1, 2, 0, 1, np.nan, 4, 4, np.nan, 3])
result = s.value_counts(normalize=True, bins=2, dropna=dropna)
expected = Series(vals, index=IntervalIndex.from_tuples(tuples))
tm.assert_series_equal(result, expected)

def test_value_counts_uint64(self):
arr = np.array([2 ** 63], dtype=np.uint64)
Expand Down