From 21135211edd30da74bba478a0e21568f8442c391 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 4 Mar 2020 22:20:03 +0100 Subject: [PATCH 1/4] sorting by index before comparing results in test_value_counts_null --- pandas/tests/base/test_ops.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index 8f48d0a3e8378..26f33c468c975 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -344,13 +344,20 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected = pd.Series(dict(counter.most_common()), dtype=np.int64) expected.index = expected.index.astype(obj.dtype) - tm.assert_series_equal(obj.value_counts(), expected) + # sort_index to avoid switched order when values share the same count + expected = expected.sort_index() + result = obj.value_counts().sort_index() + tm.assert_series_equal(result, expected) # can't use expected[null_obj] = 3 as # IntervalIndex doesn't allow assignment new_entry = pd.Series({np.nan: 3}, dtype=np.int64) expected = expected.append(new_entry) - tm.assert_series_equal(obj.value_counts(dropna=False), expected) + + # sort_index to avoid switched order when values share the same count + expected = expected.sort_index() + result = obj.value_counts(dropna=False).sort_index() + tm.assert_series_equal(result, expected) def test_value_counts_inferred(self, index_or_series): klass = index_or_series From cde9df6541f5e5c6455d703a2cb07b5a5b178cd6 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 5 Mar 2020 17:19:28 +0100 Subject: [PATCH 2/4] using sort_index for value_counts tests only for float16 --- pandas/tests/base/test_ops.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index 26f33c468c975..d23e4bbfe85fe 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -311,9 +311,10 @@ def test_value_counts(self, index_or_series_obj): if isinstance(obj, pd.MultiIndex): expected.index = pd.Index(expected.index) - # sort_index to avoid switched order when values share the same count - result = result.sort_index() - expected = expected.sort_index() + if obj.dtype == np.float16: + # TODO: Order of entries with the same count is inconsistent on CI + result = result.sort_index() + expected = expected.sort_index() tm.assert_series_equal(result, expected) @pytest.mark.parametrize("null_obj", [np.nan, None]) @@ -344,9 +345,11 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected = pd.Series(dict(counter.most_common()), dtype=np.int64) expected.index = expected.index.astype(obj.dtype) - # sort_index to avoid switched order when values share the same count - expected = expected.sort_index() - result = obj.value_counts().sort_index() + result = obj.value_counts() + if obj.dtype == np.float16: + # TODO: Order of entries with the same count is inconsistent on CI + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) # can't use expected[null_obj] = 3 as @@ -354,9 +357,11 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): new_entry = pd.Series({np.nan: 3}, dtype=np.int64) expected = expected.append(new_entry) - # sort_index to avoid switched order when values share the same count - expected = expected.sort_index() - result = obj.value_counts(dropna=False).sort_index() + result = obj.value_counts(dropna=False) + if obj.dtype == np.float16: + # TODO: Order of entries with the same count is inconsistent on CI + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) def test_value_counts_inferred(self, index_or_series): From ab04a730b8e7b6fddf7bb1a63e11c56cdc967648 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 5 Mar 2020 20:14:33 +0100 Subject: [PATCH 3/4] using sort_index for all fixture values to handle all inconsistencies on CI --- pandas/tests/base/test_ops.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index d23e4bbfe85fe..29054fb867a5b 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -311,10 +311,9 @@ def test_value_counts(self, index_or_series_obj): if isinstance(obj, pd.MultiIndex): expected.index = pd.Index(expected.index) - if obj.dtype == np.float16: - # TODO: Order of entries with the same count is inconsistent on CI - result = result.sort_index() - expected = expected.sort_index() + # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) + result = result.sort_index() + expected = expected.sort_index() tm.assert_series_equal(result, expected) @pytest.mark.parametrize("null_obj", [np.nan, None]) @@ -346,10 +345,9 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected.index = expected.index.astype(obj.dtype) result = obj.value_counts() - if obj.dtype == np.float16: - # TODO: Order of entries with the same count is inconsistent on CI - expected = expected.sort_index() - result = result.sort_index() + # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) # can't use expected[null_obj] = 3 as @@ -358,10 +356,9 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected = expected.append(new_entry) result = obj.value_counts(dropna=False) - if obj.dtype == np.float16: - # TODO: Order of entries with the same count is inconsistent on CI - expected = expected.sort_index() - result = result.sort_index() + # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) def test_value_counts_inferred(self, index_or_series): From 72b4da1ebd0558d5476f2315cc15baad43932e21 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 5 Mar 2020 20:19:22 +0100 Subject: [PATCH 4/4] only using sort_index if there are duplicated values --- pandas/tests/base/test_ops.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index 29054fb867a5b..f1cc98a1b773d 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -312,8 +312,9 @@ def test_value_counts(self, index_or_series_obj): expected.index = pd.Index(expected.index) # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) - result = result.sort_index() - expected = expected.sort_index() + if obj.duplicated().any(): + result = result.sort_index() + expected = expected.sort_index() tm.assert_series_equal(result, expected) @pytest.mark.parametrize("null_obj", [np.nan, None]) @@ -345,9 +346,11 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected.index = expected.index.astype(obj.dtype) result = obj.value_counts() - # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) - expected = expected.sort_index() - result = result.sort_index() + if obj.duplicated().any(): + # TODO: + # Order of entries with the same count is inconsistent on CI (gh-32449) + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) # can't use expected[null_obj] = 3 as @@ -356,9 +359,11 @@ def test_value_counts_null(self, null_obj, index_or_series_obj): expected = expected.append(new_entry) result = obj.value_counts(dropna=False) - # TODO: Order of entries with the same count is inconsistent on CI (gh-32449) - expected = expected.sort_index() - result = result.sort_index() + if obj.duplicated().any(): + # TODO: + # Order of entries with the same count is inconsistent on CI (gh-32449) + expected = expected.sort_index() + result = result.sort_index() tm.assert_series_equal(result, expected) def test_value_counts_inferred(self, index_or_series):