Skip to content

Commit 3abe0c1

Browse files
simplified and fixed test_value_counts
1 parent 5e1ff44 commit 3abe0c1

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

pandas/tests/base/test_ops.py

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
from datetime import datetime, timedelta
23
from io import StringIO
34
import sys
@@ -246,27 +247,15 @@ def test_nunique(self, index_or_series_obj):
246247
assert result == len(obj.unique())
247248

248249
def test_value_counts(self, index_or_series_obj):
249-
orig = index_or_series_obj
250-
obj = multiply_values(orig.copy())
251-
252-
if orig.duplicated().any():
253-
# FIXME: duplicated values should work.
254-
pytest.xfail(
255-
"The test implementation isn't flexible enough to deal"
256-
" with duplicated values. This isn't a bug in the"
257-
" application code, but in the test code."
258-
)
259-
260-
expected_index = Index(orig.values[::-1], dtype=orig.dtype)
261-
if is_datetime64tz_dtype(obj):
262-
expected_index = expected_index.normalize()
263-
expected_s = Series(
264-
range(len(orig), 0, -1), index=expected_index, dtype="int64"
265-
)
266-
250+
obj = multiply_values(index_or_series_obj)
267251
result = obj.value_counts()
268-
tm.assert_series_equal(result, expected_s)
269-
assert result.index.name is None
252+
253+
counter = collections.Counter(obj)
254+
expected = pd.Series(dict(counter.most_common()), dtype=int)
255+
expected.index = expected.index.astype(obj.dtype)
256+
if isinstance(obj, pd.MultiIndex):
257+
expected.index = pd.Index(expected.index)
258+
tm.assert_series_equal(result, expected)
270259

271260
@pytest.mark.parametrize("null_obj", [np.nan, None])
272261
def test_value_counts_unique_nunique_null(self, null_obj, index_or_series_obj):

0 commit comments

Comments
 (0)